#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    44
    Rep Power
    14

    Javascript Maths


    I am adding numbers from text boxes on a form using javascript. I was using parse int but this caused problems when adding decimals. I am know using parsefloat, this works but occasionally I will get a number that is more than 2 decimal places, is there anyway of showing the number as two decimal places.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    200
    Rep Power
    13
    var x,y,z,a;
    x = 12.345;
    y = 67.89;
    z = x*y;
    // adding 0.005 to simulate a round()
    // parseInt just chops off everything following the .
    a = parseInt(100*(z+0.005))/100;

    this will show numbers with at most 2 decimals

    when you want exactly 2 decimals, you should use strings
    possible in combination with above solution
    (convert to string, check length after dot, append 0,1,2 zero's)

IMN logo majestic logo threadwatch logo seochat tools logo