Javascript rounds math.round of and math.pow of

  • 2020-03-30 01:06:23
  • OfStack

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<title>Javascript rounded (Math.round() with 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.h pow (x, y); Returns the specified power of the base
//Returns a numerical expression in terms of x to the y, equal to x to the y
//Returns Infinity if the pow parameter is too large and causes floating point overflow
function f1(){ 
alert(Math.pow(2,10));//2 to the 10th is equal to 1024
alert(Math.pow(1024,0.1));//1024 to the 0.1 power is equal to 2
alert(Math.pow(99,9999));//Overflow returns Infinity
} 
 
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));//Three decimal places
alert(ForDight(123.99999,4));//Keep the four decimal places
} 
//Another way to round it is the same principle.
//Num is the data to be converted. N is the number of bits to be converted
//cheng(123.456,2);// Keep two decimal places  
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> 
</head> 
<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> 
</html> 

Related articles: