JavaScript code to retain N digits after the decimal point, js to retain the decimal point

Source: Internet
Author: User

JavaScript code to retain N digits after the decimal point, js to retain the decimal point

In JS, The toFixed function is usually used to retain the N digits after the decimal point.

Copy codeThe Code is as follows:
<Script language = "javascript">
Document. write ("Var a = 2.1512131231231321;
Document. write ("Original Value:" + a + "<br> ");
Document. write ("two decimal places:" + a. toFixed (2) + "<br> four decimal places" + a. toFixed (4 ));
</Script>

The rounding conversion function is as follows:

Copy codeThe Code is as follows:
Function round (v, e ){
Var t = 1;
For (; e> 0; t * = 10, e --);
For (; e <0; t/= 10, e ++ );
Return Math. round (v * t)/t;
}

In the parameters:

V indicates the value to be converted

E indicates the number of digits to be retained

The two for in the function, which is the focus,

The first for indicates the right decimal point, that is, the number of digits to the right of the decimal point;

The second for indicates the number of digits to the left of the decimal point, that is, the number of digits to the left of the decimal point.

For is used to calculate the value of t, that is, how many times the value of v should be enlarged or reduced (multiples = t ).

For uses two features in for, including conditional judgment and counter accumulation (loop ),

When e meets the conditions, and e accumulates each time (e accumulates each time, that is, the for manufacturing does not meet the cycle conditions), the t value is also calculated.

Finally, the native round method is used to calculate the v result after being enlarged/reduced, and then the result is enlarged/reduced to the correct multiple.

The following two-digit reserved instances

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Retain two decimal places
// Function: rounds a floating point number to the second place after the decimal point.
Function toDecimal (x ){
Var f = parseFloat (x );
If (isNaN (f )){
Return;
}
F = Math. round (x * 100)/100;
Return f;
}
// Two decimal places are retained. For example, if the number is 2, 00. That is, 2.00 will be added after 2.
Function todecimal (x ){
Var f = parseFloat (x );
If (isNaN (f )){
Return false;
}
Var f = Math. round (x * 100)/100;
Var s = f. toString ();
Var rs = s. indexOf ('.');
If (rs <0 ){
Rs = s. length;
S + = '.';
}
While (s. length <= rs + 2 ){
S + = '0 ';
}
Return s;
}
Function fomatFloat (src, pos ){
Return Math. round (src * Math. pow (10, pos)/Math. pow (10, pos );
}
// Rounding
Alert ("retain 2 decimal places:" + toDecimal (3.14159267 ));
Alert ("forcibly retain 2 decimal places:" + todecimal (3.14159267 ));
Alert ("retain 2 decimal places:" + toDecimal (3.14559267 ));
Alert ("forcibly retain 2 decimal places:" + todecimal (3.15159267 ));
Alert ("retain 2 decimal places:" + fomatFloat (3.14559267, 2 ));
Alert ("retain 1 decimal point:" + fomatFloat (3.15159267, 1 ));
// Five homes and six homes
Alert ("retain 2 decimal places:" + 1000.003.toFixed (2 ));
Alert ("retain 1 decimal point:" + 1000.08.toFixed (1 ));
Alert ("retain 1 decimal point:" + 1000.04.toFixed (1 ));
Alert ("retain 1 decimal point:" + 1000.05.toFixed (1 ));
// Scientific count
Alert (3.1415.toExponential (2 ));
Alert (3.1455.toExponential (2 ));
Alert (3.1445.toExponential (2 ));
Alert (3.1465.toExponential (2 ));
Alert (3.1665.toExponential (1 ));
// Precise to n bits, excluding n bits
Alert ("accurate to 2nd decimal places" + 3.1415.toPrecision (2 ));
Alert ("accurate to 3rd decimal places" + 3.1465.toPrecision (3 ));
Alert ("accurate to 2nd decimal places" + 3.1415.toPrecision (2 ));
Alert ("accurate to 2nd decimal places" + 3.1455.toPrecision (2 ));
Alert ("accurate to 5th decimal places" + 3.141592679287.toPrecision (5 ));
</Script>

The above is all the code. Is it super simple? I hope it will help you.

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.