This article analyzes a JS long integer precision problem in an example. Share it with everyone for your reference. The specific analysis is as follows:
Problem description:
There is a script function in the background, which can write scripts to dynamically call Java code
The script was executed successfully, but the results were different from the settings. This person did not receive the email
View log recharge award has been sent. roleId=10214734953631044;
This character’s ID number is 1 less, this....
Problem analysis:
It should be a JS precision issue,
Accuracy
Integers (without decimal point or exponent notation) can be up to 15 digits.
The maximum number of decimal places is 17, but floating point arithmetic is not always 100% accurate:
Modify script
Output:
1.0214734953631044E16;
This is not JavaScript’s fault, nor is it Java’s fault. The above conversion to Long is indeed roleId=10214734953631044;
Is that possible?
The javascript console inputs are all strings, and then calls Integer.valueOf or Long.valueOf conversion in java?
Solution:
Write a general conversion method to pass the character ID to JavaScriptEngine as a string
Declared as string
The execution is successful, just use it like this~
I hope this article will be helpful to everyone’s JavaScript programming design.