A summary method of rounding function in JavaScript

Source: Internet
Author: User
Tags pow

Before I was a JavaScript little white, the understanding of jquery was limited to the use of basic functions. I've been reading "JavaScript DOM programming Art" for a little bit of basic knowledge. There is a function in the instance that needs to round the result, preserving one digit after the decimal point. Then I consulted the W3school reference document and found that the number object had a tofixed () method that fully met my requirements.

ToFixed (NUM) has only one argument num, which is used to specify the number of decimal places that need to be retained, 111cn.net to 0~20 between, and throws an exception if this range is exceeded. Of course, an exception is thrown if the object calling this method is not a number.

The code is as follows Copy Code

var num = 10/3;
Alert (num.tofixed (2));


Execute the above code, you can see the output is 3.33

The num argument can be omitted, and if omitted, the default will be 0 instead, rounded to an integer.

It is worth noting that the Tofixed method may behave differently in different browsers. Like what

The code is as follows Copy Code

var money=0.00542;//0.006;
Alert (number (money). ToFixed (2));
0.00 in Ie6~7


Obviously, this method needs to be improved. Using the round method of the Math object to write a function can be solved.

The code is as follows Copy Code
A function that rounds a number
function Roundnum (number,fdigits) {
With (Math) {
Return round (Number*pow (10,fdigits))/pow (10,fdigits);
}
}
Rounding values with the Roundnum function
function Shownum () {
var num = 0.006;
Alert (Roundnum (num,2));
}


After executing the above code, we can see that the output is 0.01 and is correct. The round method returns the nearest integer to the given numeric expression. If the decimal part of number is greater than or equal to 0.5, the return value is the smallest integer greater than Otherwise, round returns the largest integer less than or equal to number.

Math.Round () and Math.pow ()

The code is as follows Copy Code

<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, WWW.111CN.NET returns 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>

<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);"/>

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.