On the loss of digital accuracy in Java object to JSON

phenomenon

Large numbers in Java, such as 18 / 19 bit integers (long), are converted into JSON. After they are output to the page, there will be a loss of precision. If the number is larger, there will be a scientific counting method.

These two problems are not the problem of JSON Toolkit (such as gson), but due to the loss of precision of large numbers stored in JS.

1. Example of precision loss: when JS expresses an integer, it can express up to 15 digits. If it exceeds 15 digits, the problem of precision loss will occur.

Reference JS document:

accuracy

Integer (without decimal point or exponential counting) can be up to 15 digits.

The last few bits become 0 and the precision is lost

var x = 1234567890123456999; console. log(x);

Output result: 123456789012345700

2. Larger numbers will appear, scientific counting

var x = 123456789012345699999999; console. log(x);

Output result: 1.234567890123457e + 23

terms of settlement

The background property is changed from numeric type to string type and converted to JSON. It will not be lost when it is transferred to the foreground.

In the above article on Java object to JSON, the loss of digital accuracy is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>