Using the tofixed function
Code to copy code 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>
Rounding the conversion function, as follows:
Code to copy code 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 convert
E indicates the number of digits to be retained
The two for in the function, this is the point,
The first for the right of the decimal point, that is, the number of digits to the right of the decimal point;
The second for the left of the decimal point, which is how many digits to the left of the decimal point.
The purpose of the for is to calculate the value of T, which is how many times the V should be enlarged or reduced (multiples =t).
For here the two features of the for are used, conditional judgment and counter accumulation (loop),
The value of T is also computed when e satisfies the condition for continuation, and e accumulates each time (the sum of E is given to a for-manufacturing condition that does not satisfy the loop).
Finally, we use the native round method to calculate the result of the enlarged/reduced V, then enlarge/reduce the result to the correct multiples.
The following kinds of reserved two-digit instances
Code to copy code as follows
<script type= "Text/javascript" >
Keep Two decimal places
Function: Rounding the floating-point number, taking the 2 digits after the decimal point
function ToDecimal (x) {
var f = parsefloat (x);
if (IsNaN (f)) {
Return
}
f = Math.Round (x*100)/100;
return F;
}
The system retains 2 decimal places, such as: 2, which will be 00 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;
(www.111cn.net) s + = '. ';
}
while (s.length <= RS + 2) {
s + = ' 0 ';
}
return s;
}
function Fomatfloat (src,pos) {
Return Math.Round (Src*math.pow (), POS)/math.pow (POS);
}
Rounded
Alert ("Keep 2 decimal places:" + todecimal (3.14159267));
Alert ("Force reserved 2 decimal places:" + toDecimal2 (3.14159267));
Alert ("Keep 2 decimal places:" + todecimal (3.14559267));
Alert ("Force reserved 2 decimal places:" + toDecimal2 (3.15159267));
Alert ("Keep 2 decimal places:" + fomatfloat (3.14559267, 2));
Alert ("Keep 1 decimal places:" + fomatfloat (3.15159267, 1));
Five homes six into
Alert ("Keep 2 decimal places:" + 1000.003.toFixed (2));
Alert ("Keep 1 decimal places:" + 1000.08.toFixed (1));
Alert ("Keep 1 decimal places:" + 1000.04.toFixed (1));
Alert ("Keep 1 decimal places:" + 1000.05.toFixed (1));
Scientific counting
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));
Accurate to n-bit, n-bit not included
Alert ("Accurate to 2nd decimal place" + 3.1415.toPrecision (2));
Alert ("Accurate to 3rd decimal place" + 3.1465.toPrecision (3));
Alert ("Accurate to 2nd decimal place" + 3.1415.toPrecision (2));
Alert ("Accurate to 2nd decimal place" + 3.1455.toPrecision (2));
Alert ("Accurate to 5th decimal place" + 3.141592679287.toPrecision (5));
</script>
From:http://www.111cn.net/wy/js-ajax/46174.htm
JS preserves the N-bit after the decimal point method Introduction