简体   繁体   中英

Why does console.log(10150141932135203) print 10150141932135204 in both Firefox and Chrome and how to I deal with large integer values like these?

I was trying to figure out why one of our clients on Facebook was having issues and I traced it to the number 10150141932135203 turning into 10150141932135204 giving us rather unexpected results.

$ node
> 10150141932135203
10150141932135204
> 10150141932135204
10150141932135204
> 10150141932135205
10150141932135204
> 10150141932135206
10150141932135206
> 10150141932135207
10150141932135208
> 10150141932135208
10150141932135208
> 10150141932135209
10150141932135208
> 10150141932135210
10150141932135210

How can I deal with integer numbers of this size?

If the numbers are bigger than what the IEEE 754 spec allows (2 53 ), they will lose precision as your examples demonstrate.

You could use a Binary Coded Decimal library for JavaScript, eg BCMathJs .

This, of course, is only applicable if you need to perform arithmetic on the numbers. If not, keep them as strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM