This article mainly introduces the JS long integer precision problem. The example analyzes the long integer precision problem and the corresponding solution of the Java project combined with the front-end js script, which has some reference value, for more information about JS long integer precision, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
Problem description:
There is a script function in the background that allows you to write scripts to dynamically call Java code.
The Code is as follows:
ImportClass (com. ztgame. center. controller. api, P360ApiController );
Var roleId = 10214734953631045;
P360ApiController. notice (roleId, 4 );
The script is successfully executed, but the running result is different from the setting. This person does not receive an email.
View the recharge award has been sent. roleId = 10214734953631044;
This role ID is 1 less, and this ....
Problem Analysis:
It should be about JS precision,
Precision
An integer (without the decimal point or exponential Notation) can contain up to 15 digits.
The maximum number of decimal places is 17, but the floating point operation is not always 100% accurate:
Modify script
The Code is as follows:
Var roleId = 10214734953631045;
Var output = roleId;
Output:
1.0214734953631044E16;
This is not a JavaScript error, nor a Java error. The preceding conversion to Long is indeed a roleId = 10214734953631044;
Can that be the case?
In the javascript console, all input strings are input, and then the Integer. valueOf or Long. valueOf in java is called for conversion?
Solution:
Write a general conversion method to upload the role ID to JavaScriptEngine using a string
The Code is as follows:
ImportClass (com. ztgame. common. util. StringKit );
ImportClass (com. ztgame. center. controller. api, P360ApiController );
Declare as a string
The Code is as follows:
Var roleId = "10214734953631045 ";
In this way, the JavaScriptEngine obtains the string, and the job is actually processed by Java.
The Code is as follows:
P360ApiController. notice (StringKit. parseLong (roleId), 4 );
The execution is successful. Use it like this ~
I hope this article will help you design javascript programs.