Click to See Complete Forum and Search --> : 74.99 * 100 = 74.98.99999 ?
I am having problems with multiplication in JS. I have some code that at one point multiplies a variable by 100. The problem is when the variable is 74.99 (and it sometimes happens with other values) that the result is 7498.99999999 instead of 7499. Try it yourself at this nifty JS eval form I found: http://www.danbbs.dk/~erikoest/js_eval.htm
What am I doing wrong? (Or rather, how do I code around this bug?)
Thanks,
-Ryan
96turnerri
11-10-2003, 11:23 AM
dont know why it does that, you could try setting the dp to 2 then it will round up to 7499 or whatever
gil davis
11-10-2003, 11:23 AM
This problem is caused by javascript's floating point math package. When decimal numbers are converted before the math is performed, the accuracy of the result suffers. The way around it would be to not use decimal numbers - that may be hard to do. However, if there is a way to do it, then just place the decimal in your display to the user after all the computations.
It seems to work for me if I use x * 10 * 10 instead of just taking it * 100. See any problems with this?
Thanks,
-Ryan
requestcode
11-10-2003, 11:43 AM
This short tutorial on rounding might help:
http://www.javascriptkit.com/javatutors/round.shtml