<script type= "Text/javascript" >//keep two decimal places //function: Rounding the floating-point number, taking the 2 digits after the decimal point functionToDecimal (x) {varf =parsefloat (x); if(IsNaN (f)) {return; } f= Math.Round (x*100)/100;returnF; } //The system retains 2 decimal places, such as: 2, which will be 00 after 2. That is 2.00 functionToDecimal2 (x) {varf =parsefloat (x); if(IsNaN (f)) {return false; } varf = Math.Round (x*100)/100;vars =f.tostring (); varrs = S.indexof ('. ')); if(Rs < 0) {RS=s.length; S+ = '. '; } while(s.length <= RS + 2) {s+ = ' 0 '; } returns; } functionfomatfloat (src,pos) {returnMath.Round (Src*math.pow (), POS)/math.pow (POS); } //RoundingAlert ("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 intoAlert ("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 CountingAlert (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 includedAlert ("Precision to decimal point 2nd" + 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>
JavaScript retains two decimal places