McKirahan wrote:
Quote:
How do I add floating point numbers accurately?
Using '+'.
|
A simpler demonstration of your problem:
alert(136.57+54.83);
shows 191.39999999999998
Quote:
Javascript numbers are represented in binary as IEEE-754 Doubles, with
a resolution of 53 bits, giving an accuracy of 15-16 decimal digits;
integers up to about 9e15 are precise, but few decimal fractions are. |
in this case 136.57 is stored internally as 136.56999999999999,
54.83 is stored internally as 54.829999999999998.
Adding these together and reducing the result to the appropriate precision
gives the answer displayed above.
If you want to display a friendlier value as the result, change your final
alert to:
alert(t.toFixed(2) + " = 191.40 ?");