Example Analysis of JS long integer precision _ javascript tips-js tutorial

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.