Thursday, July 03, 2008

Javascript bug

Try typing this in your IE/Firefox:

javascript:alert(174.02+10.08);

By right, 174.02 + 10.08 = 184.1

But instead you'll end up with 184.10000000000002!

Crikey, it simply doesn't add up!

(Edit: Of course then we should have a work around in this case. To which, I'd resorted to doing the calculation first, then performing a rounding function on the result. The rounding should chop off the excess to the x decimal point, where x is whatever rocks your boat. I chose 3, since it's one more than the regular dollars and cents.)

No comments: