The solution of a long decimal point when adding and subtracting JS small number

Source: Internet
Author: User

1. Questions:

This happens in the usual code, and the result of parsefloat (11.3-10.1) operation is still 1.200000000000001.

code example:

var arr = [0.0111,11.002,0.3,1.5,1.61var 0 = ={     + + Item   })   Console.log (total)    

The result of the return---14.423100000000002, which is illogical.

2. Solution:

Here's a practical workaround for some floating-point application functions in math such as: parsefloat () and Math.floor ()

The code is as follows:

var arr = [0.0111,11.002,0.3,1.5,1.61var 0 = ={        = parsefloat (item    )}    )= Math.floor (total* 100       )/    console.log (total)

The returned result---14.42, which returns a number with a two-digit decimal point.

Of course there are other solutions, this article only records this one, as a summary and later reference.

The solution of a long decimal point when adding and subtracting JS small number

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.