Javascript format number keep two decimal point example code _ javascript skills

Source: Internet
Author: User
There are many ways to retain two decimal places in tabular numbers. The following article will introduce how to use js to implement them in detail: multiple methods for formatting data in JavaScript to retain two decimal places

Best method:

Keep the two. It seems like this is the case.

The Code is as follows:


Var a = 9.39393;
Alert (a. toFixed (2 ));


Note:

Alibaba (Number. toFixed (9.39393 ));

9.39 is returned.
However, only ie5.5 or later versions are supported.

Other methods:

Function roundFun (numberRound, roundDigit) // rounding, the number of reserved digits is roundDigit {if (numberRound> = 0) {var tempNumber = parseInt (numberRound * Math. pow (10, roundDigit) + 0.5)/Math. pow (10, roundDigit); return tempNumber;} else {numberRound1 =-numberRound var tempNumber = parseInt (numberRound1 * Math. pow (10, roundDigit) + 0.5)/Math. pow (10, roundDigit); return-tempNumber ;}}

Method 2:

《script》  tmp = "1234567.57232"  result = tmp.substr(0,tmp.indexOf(".")+3);  alert(result);  《script》 

Method 3:

The Code is as follows:


Var a = 3.1415926;
A = a. toFixed (2); // retain 2 bits, but the result is of the String type.
A = parseFloat (a); // convert the result to float
// Use the following steps:
A = parseFloat (a. toFixed (2 ));

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.