Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> floatDecimal.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
<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
Document. writeln ("retain 2 decimal places:" + toDecimal (3.14159267) + '<br/> ');
Document. writeln ("forcibly retain 2 decimal places:" + todecimal (3.14159267) + '<br/> ');
Document. writeln ("retain 2 decimal places:" + toDecimal (3.14559267) + '<br/> ');
Document. writeln ("forcibly retain 2 decimal places:" + todecimal (3.15159267) + '<br/> ');
Document. writeln ("retain 2 decimal places:" + fomatFloat (3.14559267, 2) + '<br/> ');
Document. writeln ("retain 1 decimal place:" + fomatFloat (3.15159267, 1) + '<br/> ');
// Five homes and six homes
Document. writeln ("retain 2 decimal places:" + 1000.003.toFixed (2) + '<br/> ');
Document. writeln ("retain 1 decimal point:" + 1000.08.toFixed (1) + '<br/> ');
Document. writeln ("retain 1 decimal point:" + 1000.04.toFixed (1) + '<br/> ');
Document. writeln ("retain 1 decimal point:" + 1000.05.toFixed (1) + '<br/> ');
// Scientific count
Document. writeln (3.1415.toExponential (2) + '<br/> ');
Document. writeln (3.1455.toExponential (2) + '<br/> ');
Document. writeln (3.1445.toExponential (2) + '<br/> ');
Document. writeln (3.1465.toExponential (2) + '<br/> ');
Document. writeln (3.1665.toExponential (1) + '<br/> ');
// Precise to n bits, excluding n bits
Document. writeln ("precise to decimal place 2nd" + 3.1415.toPrecision (2) + '<br/> ');
Document. writeln ("precise to decimal place 3rd" + 3.1465.toPrecision (3) + '<br/> ');
Document. writeln ("precise to decimal place 2nd" + 3.1415.toPrecision (2) + '<br/> ');
Document. writeln ("precise to decimal place 2nd" + 3.1455.toPrecision (2) + '<br/> ');
Document. writeln ("precise to decimal place 5th" + 3.141592679287.toPrecision (5) + '<br/> ');
</Script>
</Head>
<Body>
This is my HTML page. <br>
</Body>
</Html>
Javascript rounded to retain two decimal places
Copy codeThe Code is as follows:
Function count (){
// Alert ("count ");
Var size = ~~ (Document. getElementById ("size"). value );
Var value = 0;
For (var I = 0; I <size; I ++ ){
Var val = 1 * (document. getElementById ("afterAdjScor" + I). value );
If (null! = Val ){
Value = Math. round (1 * (value + val) * 100)/100;
}
}
If (isNaN (value )){
Value = "the input must be numeric ";
}
Document. getElementById ("total"). value = value;
}
Note :~~ XX: Convert string to int
1 * XX: Convert string to float
Math. round (1 * (value + val) * 100)/100: Rounding