Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>javascript Rounding (Math.Round () and Math.pow ()) </title>
<script type= "Text/javascript" >
Math.Round (x); Returns the nearest integer to the number, rounded to the integer, that is, to the decimal part
function f () {
Alert (Math.Round (123.567));
Alert (Math.Round (123.456));
}
Math.pow (x,y); Returns the specified power of the base
Returns the numeric expression with the Y-power of x equal to the Y-power of X
If the POW parameter is too large to cause a floating-point overflow, return the Infinity
Function F1 () {
Alert (Math.pow (2,10));//2 10 times equals 1024.
Alert (Math.pow (1024,0.1));//1024 0.1 times equals 2.
Alert (Math.pow (99,9999));//Overflow returns infinity
}
/*javascript sets the number of decimal places to keep, rounded.
*fordight (dight,how): Numeric formatting functions, dight the number to be formatted, how many decimal places to keep.
* The method here is to multiply by 10, then remove the decimal, and then divide by the multiples of 10.
*/
function Fordight (dight,how) {
Dight = Math.Round (Dight*math.pow (10,how))/math.pow (10,how);
return dight;
}
function F2 () {
Alert (Fordight (12345.67890,3));//Keep three decimal digits
Alert (Fordight (123.99999,4));//keep four decimal digits
}
Another method of rounding is the same principle.
Inside the two parameters: num is the data to be converted. n is the number of digits to convert
Cheng (123.456,2);//Keep two decimal digits
Function Cheng (num,n) {
var dd=1;
var tempnum;
for (i=0;i<n;i++) {
dd*=10;
}
Tempnum = NUM*DD;
Tempnum = Math.Round (tempnum);
alert (TEMPNUM/DD);
}
</script>
<body>
<input type= "button" value= "Round" onclick= "f ();"/>
<input type= "button" value= "POW" onclick= "F1 ();"/>
<input type= "button" value= "sets the number of decimal places to keep, rounding" onclick= "F2 ();"/>
<input type= "button" value= "Cheng" onclick= "Cheng (123.456,2);"/>
</body>