简体   繁体   English

为什么console.log(10150141932135203)在Firefox和Chrome中打印10150141932135204以及如何处理像这样的大整数值?

[英]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. 我试图找出为什么我们在Facebook上的一个客户遇到问题,我追溯到10150141932135203转向10150141932135204,给我们带来了意想不到的结果。

$ 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. 如果数字大于IEEE 754规范允许的数字(2 53 ),那么它们将失去精确度,如您的示例所示。

You could use a Binary Coded Decimal library for JavaScript, eg BCMathJs . 您可以为JavaScript使用二进制编码的十进制库,例如BCMathJs

This, of course, is only applicable if you need to perform arithmetic on the numbers. 当然,这只适用于需要对数字进行算术运算的情况。 If not, keep them as strings. 如果没有,请将它们保留为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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