JS rounding function toFixed (), the parameter is the number of digits reserved decimal. <script language="JavaScript">document.write (""); varA=2.1512131231231321; document.write ("The original value:"-am"<br>"); document.write ("two-bit decimal point:"+a.tofixed (2)+"<br> four-bit decimal point"+a.tofixed (4)); </script
<! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en"> "keywords"Content="Keyword1,keyword2,keyword3"> <meta http-equiv="Description"Content="This is my page"> <meta http-equiv="Content-type"Content="text/html; Charset=utf-8"> <script type="Text/javascript">//keep two decimal places//function: Rounding the floating-point number, taking the 2 digits after the decimal pointfunction ToDecimal (x) {varf =parsefloat (x); if(IsNaN (f)) {return; } f= Math.Round (x* -)/ -; returnF; } //The system retains 2 decimal places, such as: 2, which will be 00 after 2. That is 2.00function ToDecimal2 (x) {varf =parsefloat (x); if(IsNaN (f)) {return false; } varf = Math.Round (x* -)/ -; vars =f.tostring (); varrs = S.indexof ('.'); if(Rs <0) {RS=s.length; S+='.'; } while(S.length <= RS +2) {s+='0'; } returns; } function Fomatfloat (src,pos) {returnMath.Round (Src*math.pow (Ten, POS))/math.pow (Ten, POS); } //RoundingAlert ("Keep 2 decimal places:"+ ToDecimal (3.14159267)); Alert ("Force 2 decimal places to be reserved:"+ ToDecimal2 (3.14159267)); Alert ("Keep 2 decimal places:"+ ToDecimal (3.14559267)); Alert ("Force 2 decimal places to be reserved:"+ 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 ("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> This isMy HTML page. <br> </body>
JS Rounding function