A long list of decimal places appears in JavaScript decimal subtraction

Source: Internet
Author: User

We want to modify the display format of a page's data, which requires two steps:

1. In JS through $ ('. Class1. Class2 Li:eq (2) Span.value '). Text (). Trim (); A similar statement gets the contents of the data.

2. Modify the data to the desired format. (eg:data=10 ==> 9+1)

If we get the data value is an integer, then no problem, things will go well, as expected results.

But if you take a decimal, the problem comes.

What is this for?

A: This is related to the data structure, the integer type automatically converted to positive type calculation, decimal type directly into the double type calculation, this is in memory operation must be so, you should know that the computer only know 0 and 1 bar, specifically the floating point accuracy problem, float accurate to the decimal point after the 7-bit double Accurate to 15 digits after the decimal point.

Knowing the cause of the problem, we just need to do a simple answer to the reserved decimal number operation, namely:

Data.tofixed (2);

The number of decimal digits is in the parentheses.

A long list of decimal places appears in JavaScript decimal subtraction

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.