PDA

View Full Version : parseFloat function does not work as intended



darshanK
1 Aug 2013, 6:29 AM
I am trying to parse a large string "99999999999998.99" into float. But this is not working as expected.The resultant is 99999999999998.98.

We see minor fluctuation after the decimal point. This is happening only for the large numbers. Any idea why is this happening, is theres any work around to avoid this.

eric.cook
1 Aug 2013, 8:01 AM
It has to do with the technical details involving JavaScript and IEEE-754 floating point numbers. There's a limited amount of precision available and not every number can be exactly represented (e.g., 0.1).

If you need exact precision on arbitrary length numbers, you should look into some form of big number library. I don't think there's anything you can do about it otherwise.

tobiu
1 Aug 2013, 8:04 AM
This is related to the way the floating point datatype works.

It saves its data in a format like 1.234e-10.

Since there is a fixed amount of characters before the e, it can never be 100% precise.

Best regards
tobiu