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" ""> 
<html xmlns="" > 
<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(){ 
//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; 
tempnum = num*dd; 
tempnum = Math.round(tempnum); 
<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);" /> 

Related articles: