JS reserved two decimal places

Source: Internet
Author: User

Today when you make a Birt report, you want to display a list of percentages of data, but for some special reasons, you cannot use the percent style, which means that if the data is 0.9538, it cannot be displayed as a "95.38%" style and must be displayed as "95.38".

To start with JavaScript's built-in function, Math.Round (), Math.Round () can only be displayed as integers, not decimals.

Again online search, or use Math.Round (), written math.round (x*100)/100, of course I want to Math.Round (x*100), it does not matter. But the direct use of Math.Round (x*100) There is a problem, sometimes there is a small error, showing a lot of bits of the decimal place, such as 0.9996*100, will become 99.96000000000001, this is not what I want.

To find the functionality of JavaScript, the number type has a tofixed () function, which tests:

Alert ((0.9996*100). toFixed (2));

  

Get 99.96, this is what I want!

Use Number.tofixed () to display any number of decimal digits in the format numbers!

JS reserved two decimal places

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.