Json max int数 [英] Json max int number

查看:83
本文介绍了Json max int数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,如何从json中获取数字,为什么数字的末尾会出现零?

Tell me please, how do I get the number out of the json, why do zeros appear at the end of the number?

var a = '{ "n": 870744036720833075 }';
var json = JSON.parse(a);

document.getElementById('test').innerHTML = json.n; // 870744036720833000 why?

我该如何解决?

推荐答案

该整数超出了Javascript认为的安全整数.您可以通过运行Number.isSafeInteger(870744036720833075)进行测试,这将返回false. 因此,数字以最大的精度存储.

This integer exceeds what Javascript considers a safe integer. You can test this by running Number.isSafeInteger(870744036720833075), which will return false. Because of this, the number is stored to the maximum available precision.

请参见此处最大安全整数的说明.在文档中可以注意到它的副作用.

See here for an explanation of max safe integers. A sideeffect of this can be noticed in the documentation.

在此上下文中,安全是指表示整数的能力 准确地进行比较.例如, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 评估为true,这在数学上是不正确的.看 Number.isSafeInteger()了解更多信息.

Safe in this context refers to the ability to represent integers exactly and to correctly compare them. For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true, which is mathematically incorrect. See Number.isSafeInteger() for more information.

这意味着从Javascript的角度来看,您正在创建的数字及其给出的数字将与同一事物进行比较.

This means that from Javascript's point of view, the number you are creating, and the number it gives you would compare to the same thing.

更新

要存储数字,您将不得不将其视为字符串.将您的json更改为var a = '{ "n": "870744036720833075" }';

To store the number, you will have to treat it as a string. Change your json to be var a = '{ "n": "870744036720833075" }';

如果需要对它们执行数学运算,则可以找到一些对字符串编码的数字执行数学运算的Javascript库,例如

If you need to perform mathematical operations on them, you can find some Javascript libraries that perform mathematical operations on string encoded numbers, such as strint, or the ones suggested in comments.

更新

如果要从外部api获取此信息,则必须自己解析JSON. 答案可能对您来说是一个好的开始.

If you are getting this from an external api, you will have to parse the JSON yourself. This answer might be a good start for you.

这篇关于Json max int数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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