Does Parse Float have a rounding limit? How can i fix this?

I created a system that parses a compact string of data in JSON. I use a 19-digit number to store identifiers. Unfortunately, any number greater than 17 digits, parseFloat() rounds the last few digits.

This breaks the entire row of data. Can i fix this?

For example, 8246295522085275215 turns into 8246295522085276000 . Why is this?

http://jsfiddle.net/RobertWHurst/mhZ7Q/

+8
javascript math parsefloat
source share
3 answers

JavaScript has only one numeric type, which is the bidirectional precision of the IEEE 754 floating point . This means that you have a maximum of 52 bits of accuracy, which is just over 15 decimal places.

If you need higher precision, you should use the bignum library or work with strings.

+12
source share

Numbers in JavaScript lose accuracy if they are above a certain value.

According to http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference , integers are only reliable up to 15 digits ( 9 * 10^15 , to be precise).

+3
source share

Try one of these 1. Use line 2. Divide your number in half and save the smaller parts of the array 3. Bignum library 4. Use a smaller number if you can

+1
source share

All Articles