简体   繁体   English

使用 JSON.parse 解析数量

[英]Parsing amount with JSON.parse

Dos it exist a maximum float number with two digits after decimal point that can be parsed with JSON.parse without losing precision?它是否存在可以用 JSON.parse 解析而不会丢失精度的小数点后两位数的最大浮点数?

For example:例如:

JSON.parse('{"amount": 9999999999999.99}')

{amount: 9999999999999.99} // not lose (probably) {amount: 9999999999999.99} // 不输(大概)

JSON.parse('{"amount": 99999999999999.99}')

{amount: 99999999999999.98} // lose {金额:99999999999999.98} // 输

If x is a decimal floating-point number with 15 significant digits or fewer, then converting x to JavaScript's Number type and then converting the result back to a decimal floating-point number with the same number of significant digits produces exactly x , provided the number is within normal bounds.如果x是用15显著位或更少的十进制浮点数,然后转换x至JavaScript的数量的类型,然后将所述结果返回给一个十进制浮点数具有相同数量的显著位数产生准确的x,所提供的数是在正常范围内。 Therefore, all decimal numerals with two digits after the decimal point from “.00” to “9999999999999.99” can be parsed, stored, and reformatted with two digits after the decimal point, and the result will be the original numeral.因此,所有从“.00”到“9999999999999.99”小数点后两位的十进制数字都可以解析、存储并重新格式化为小数点后两位,结果将是原始数字。

The stored value will generally not equal the original value.存储的值通常不等于原始值。 For example, when “.99” is parsed, the result will be exactly 0.9899999999999999911182158029987476766109466552734375.例如,当解析“.99”时,结果将正好是 0.9899999999999999911182158029987476766109466552734375。 However, the stored value will be sufficiently close to the original value that, when converted back to the original number of digits, the original value is recovered.然而,存储的值将足够接近原始值,当转换回原始位数时,原始值被恢复。 Note that you must know the original number of digits;请注意,您必须知道原始数字的位数; it is not inherently a part of the Number value.它本质上不是 Number 值的一部分。

15 is a lower bound for this property. 15 是此属性的下限。 There may be some exponent values for which all 16-digit decimal numerals survive a round trip.可能存在一些指数值,所有 16 位十进制数字都可以在往返过程中幸存下来。 However, since 99999999999999.99 (16 digits) produces 99999999999999.98, we know this is not one of those intervals.然而,由于 99999999999999.99(16 位)产生 99999999999999.98,我们知道这不是这些区间之一。

If you want to know the specific number between 9999999999999.99 and 99999999999999.99 where this round-trip property first fails, you may have to hunt for it computationally.如果您想知道 9999999999999.99 和 99999999999999.99 之间的特定数字,该往返属性首先失败,您可能需要通过计算来寻找它。 It many not be a value that is easy to calculate directly by mathematical properties.它很多不是一个容易通过数学属性直接计算的值。

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

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