大数字上的JSON.Stringify()会更改数字值吗? [英] JSON.Stringify() on big numbers alters the numeric value?

查看:164
本文介绍了大数字上的JSON.Stringify()会更改数字值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务操作,该操作返回具有longList<string>属性的对象.当我在WCF应用程序中测试操作时,一切正常,并且值正确.但是,我需要能够使用jQuery和JSON格式调用该服务.当我在OnSucceed函数中读回它时,long属性的值显然会改变.

I have a WCF service operation that returns an object with long and List<string> properties. When I test the operation in a WCF application, everything works fine and the values are correct. However, I need to be able to call the service using jQuery and JSON format. The value of the long property apparently changes when I read it back in the OnSucceed function.

搜索后,我发现JSON.stringify会更改较大的值.因此,在这样的代码中:

After searching I've found that JSON.stringify changes big values. So in code like this:

alert(JSON.stringify(25001509088465005));

...它将显示为25001509088465004.

发生了什么事?

此处演示: http://jsfiddle.net/naveen/tPKw7/

推荐答案

JavaScript使用 IEEE- 754 双精度(64位)格式.据我了解,这将为您提供53位精度,或者15至16个十进制数字.您的电话号码所包含的位数超出了JavaScript所能应付的位数,因此最终得到了一个近似值.

JavaScript represents numbers using IEEE-754 double-precision (64 bit) format. As I understand it this gives you 53 bits precision, or fifteen to sixteen decimal digits. Your number has more digits than JavaScript can cope with, so you end up with an approximation.

您需要对这个大数进行数学运算吗?因为如果它只是某种ID,则可以将其作为字符串返回,从而避免了该问题.

Do you need to do maths operations on this big number? Because if its just some kind of ID you can return it as a string and avoid the problem.

这篇关于大数字上的JSON.Stringify()会更改数字值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆