JavaScript rounding Math.Round () with Math.pow () Introduction _javascript Tips

Source: Internet
Author: User
Tags pow
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.