Javascript number addition error

Discussions

General J2EE: Javascript number addition error

  1. Javascript number addition error (4 messages)

    This seems stupid but I am not able to get the point here. Well the problem is..
    I have a stored procedure that returns numbers with a 2 digit precision (the stored Procedure rounds them). These are then displayed in textbox on a JSP page. I am then adding these values using javascript as given below.

    var sum =0;
    for(i=0;i<len;i++)
          sum = sum+parseFloat(obj[i].value);

    where len is the number of textboxes on the form. I was expecting that the variable sum should have a 2 digit precision number in the end as the stored procedure gives out just 2 digits after the decimal point. However the sum is having something like

    95.519999999999 (not the same number of 9's though). Can someone tell me what am I doing wrong? and is there a work around for this. Pls note I don't want to round the sum since I am doing it already with the stored procedure.

    Thanks is advance!

    Threaded Messages (4)

  2. Floating points[ Go to top ]

    Floating point precision is always a pain. But since we know you're dealing with 2 digit precision numbers, you can avoid the problem by multiplying your values by 100, and dividing your final sum by 100 so that you're summing integers instead of floats:

    var sum = 0;
    for(i=0;i<len;i++)
      sum += (100 * parseFloat(obj[i].value));
    sum /= 100
  3. Floating points[ Go to top ]

    Hi Joe,
          Thanks for the reply. That works fine, but don't you think its like applying round function manually? Do you know why the normal + in javascript creates precision longer than the values on either side of it?
    If nothing by the end of the day, I am going to go with your fix. Thanks again.
  4. Floating points[ Go to top ]

    So, did you guys know why exactly this issue is happening.

    At a key part of one of the systems I've built, I've used JS in the past and it is causing the same issue. In my case, I can't do the rounding because i need to admit any number being posted.

    The funny thing, it is doing the problem with certain number only.

    Here is an example;

    I use parseFloat to convert the numbers obviosly. I take 115.04 + 15 and the sum comes out as 130.04000000000002

    If I use 0.03 or 0.05 or any other decimal that I've tried, it worked just fine. It is an extraordinary random issue. I can't seem to figure out why or how it is happening.

    Any other suggestions?

    Cheers,
    Mert
  5. It's correct as per IEEE and ECMA[ Go to top ]

    This is because the number you have cannot be converted into an exact binary, so IEEE standard for floating point Math says that 'technically' correct answer for 115.04 + 15 is 130.04000000000002 You can't really do much here, other than writing your own rounding code.. Excel and windows calculator do an intelligent rounding off and give you 'arithmatically correct' answer.. Try 81.66*15 in IE or Firefox and you will get 1224.89999999999 rather than the expected 1224.90