The number of decimal places reserved by hookan

Source: Internet
Author: User

Today, I found Baidu and Mu. I checked Google and Mu. I asked my friend and told me that I had done it myself and finally got it. Haha

Everyone has noticed that, in fact, our law knows that it is wrong:

Note:

The function used to retain two decimal places in Javascript is tofixed (2)

However, when this function is combined with the "%" sign, it will be confused and will not be heard. Please refer to the following example for comparison:

Example 1:

VaR Ss = (parsefloat (AA [0])/parsefloat (AA [1]). tofixed (2 );

Alert (SS * 100 + "% ");

It is very likely that 54.5000000001% of the data will show this effect.

 

Example 2:

VaR Ss = (parsefloat (AA [0])/parsefloat (AA [1]) * 100;

Alert (ss. tofixed (2) + "% ");

Then, the result is what you want.

 

What are the causes of these two differences?

In fact, it's just the first person in the trap,

If we keep two decimal places and use the tofixed () method, multiply the number by 100, then the effect we don't want will appear.

If we multiply the value by 100 and use the tofixed () method to keep two decimal places, we will not get the effect we don't want!

I'm a cainiao. Please see me .... Haha

 

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.