https://www.vevb.com/article/154898.htm
Below we will introduce the implementation method of JavaScript to retain two decimal implementation methods:
rounding
The following processing results will be four -way:
var num = 2.446242342; num = num.tofixed (2); // The output result is 2.45
No four -way entry
The results of the following processing will not go four houses:
The first one is to put the decimal side integer first:
Math.floor (15.7784514000 * 100) / 100 // output result is 15.77
The second type is used as a string, using regular matching:
Number (15.7784514000.tstring (). Match (/^/d+(?: /./ d {0,2})?/) // The output result was 15.77, which cannot be used for integer, such as 10.0000, 10.0000Note: If it is negative, please convert it to a positive number before calculating, and finally turn back to negative numbers
JavaScript retains the example of two decimal numbers:
<script type = "text/javascript"> // Reserves two decimal numbers // Function: Put the floating point between four houses and five, and after taking the decimal point, the two -bit function todecimal (x) {var F = PARSEFLOAT (x); if (isnan (f) ) {RETURN;} f = math.round (x*100)/100; Return f;} // The system retains 2 decimal numbers, such as: 2, which will make up after 2, that is, 2.00 function todeCimal2 (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);} // Four Hinds and Wutong ("Keep 2 decimal decimal: + TODECIMAL ( + TODECIMAL ( 3.14159267); Alert ("Mandarin reserved 2 decimal numbers:" + TODECIMAL2 (3.14159267)); Alert ("Reserve 2 decimal: + TODECIMAL (3.14559267)); Alert (" Mandatory 2 digits: " + TODECIMALAL 2 (2 ( 3.15159267)); Alert ("Keep 2 decimal numbers: + Fomatfloat (3.14559267, 2)); Alert (" Reserve 1 decimal: + fomatfloat (3.15159267, 1)); // Two -bit decimal: " + 1000.003.tofixed (2)); Alert (" Reserve 1 digits: + 1000.08.tofixed (1)); Alert ("Reserve 1 digits:" + 1000.04.tofixed (1)); Alert ("Reserve 1 digit decimal:" + 1000.05.tofixed (1)); // Scientific count the alert (3.1415.toexponential (2)); Alert (3.1455.toexponential (2)); Alert (3.1445.Toexponential ( 2) );; 3.1465.toexponential (2)); Alert (3.1665.toexponential (1)); // Precise to N position, excluding n bit alert ("Precise to the 2nd bit of decimal point" + 3.1415.toprecision (2) ); Lirt ("Precise to the 3rd of the decimal point" + 3.1465.toprecision (3)); alert ("accurate to the 2nd place of the decimal point" + 3.1415.toprecision (2)); alert + 3.1455.toprecision (2)); Alert ("precise to the 5th point of the decimal point" + 3.141592679287.toprecision (5)); </script>Use JavaScript to take the Float decimal point after two points. For example, 22.127456 is taken to 22.13. How to do it?
1. Discard the decimal part and keep the integer part
Parseint (5/2)
2. Take up upwards, and add a decimal to the integer part to add 1
Math.ceil (5/2)
3, four houses and five enters.
Math.round (5/2)
4, take down
Math.floor (5/2)
Alternative method
1. The most stupid way
Copy code code as follows:
Function get ()
{{
var s = 22.127456 + "" ";
var str = s.substring (0, s.InDexof (".") + 3);
alert (str);
}
2. Good expression of regular expressions
Copy code code as follows:
<script type = "text/javascript">
onLoad = Function () {
var a = "23.456322";
Var Anew;
var Re = /( #+9 /.9> 2 })**)-2:
ANEW = A.Replace (Re, "$ 1");
Alert (ANEW);
}
</script>
3. He is smarter ...
Copy code code as follows:
<script>
var num = 22.127456;
alert (math.round (num*100)/100);
</script>
4. Friends who will use fresh things ... but you need IE5.5+to support it.
5. JS retains 2 decimal (mandatory)
For decimal points greater than 2 bits, the above function is fine, but if it is less than 2 bits, such as: ChangetWodeCIMAL (3.1), it will return 3.1. If you must need a format of 3.10, then the following function needs:
Copy code code as follows:
Function ChangetWodeCimal_f (x) {
var f_x = Parsefloat (x);
if (isnan (f_x)) {
Alert ('Function: Changetwodecimal-> Parameter E error');
Return false;
}
var f_x = math.round (x * 100) / 100;
var s_x = f_x.tostring ();
var pos_decimal = s_x.indexof ('.');
if (POS_DECIMAL <0) {
pOS_DECIMAL = s_x.Length;
s_x += '.';
}
While (S_X.Length <= POS_DECIMAL + 2) {{
s_x += '0';
}
Return s_x;
}
Function: Put the floating point and go four houses and five, take 2 digits after the decimal point.
This function returns the format usage of the string: CHANGETWODECIMAL (3.1415926) Return 3.14 CHANGETWODECIMAL (3.1) Return 3.10 3.10