There are many rounding functions in js, such as toFixed and Math. round () and Math. pow () and other functions. The following articles mainly introduce toFixed and Math. round () and Math. do not miss out on the four functions of pow.
Previously, I was a little JavaScript, and my understanding of jQuery was limited to the use of basic functions. I recently read "JavaScript DOM programming art", and I am learning a little basic knowledge. There is a function in the instance that needs to round the result and retain the number after the decimal point. Then I checked the W3School reference document and found that the Number object has a toFixed () method, which fully meets my requirements.
ToFixed (num) has only one parameter, num, which specifies the number of decimal places to be retained. The value of bKjia. c0m is 0 ~ 20. If the range is exceeded, an exception is thrown. Of course, if the object that calls this method is not a Number, an exception is thrown.
| The Code is as follows: |
Copy code |
Var num = 10/3; Alert (num. toFixed (2 )); |
Run the above Code. The output result is 3.33.
The num parameter can be omitted. If omitted, 0 is used by default, that is, rounding is an integer.
It is worth noting that the toFixed method may behave differently in different browsers. For example
| The Code is as follows: |
Copy code |
Var money = 0.00542; // 0.006; Alert (Number (money). toFixed (2 )); // In IE6 ~ 0.00 for 7 |
Obviously, this method needs to be improved. You can use the Round method of the Math object to write a function.
| The Code is as follows: |
Copy code |
// The function that rounds the value Function roundNum (number, fDigits ){ With (Math ){ Return round (number * pow (10, fDigits)/pow (10, fDigits ); } } // Use the roundNum function to round the value Function showNum (){ Var num = 0.006; Alert (roundNum (num, 2 )); }
|
After executing the above code, we can see that the output result is 0.01, which 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 number. Otherwise, round returns the maximum 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, rounded to an integer, that is, 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 number. // Returns a numeric expression with the y Power of x, equivalent to the y Power of x. // If the pow parameter is too large and causes floating point overflow, www.111cn. Net returns Infinity. Function f1 (){ Alert (Math. pow (1024); // The 10th power of 2 equals Alert (Math. pow (1024, 0.1); // The Power of 1024 equals 2 Alert (Math. pow (99,9999); // Infinity is returned if the overflow occurs. } /* Set the number of decimal places to be retained in Javascript. * ForDight (Dight, How): a numeric Formatting Function. It refers to the number to be formatted and the number of decimal places to be retained. * The method here is to multiply the number by a factor of 10, remove the decimal number, and then divide the number by a factor 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); // retain three decimal places Alert (ForDight (123.99999, 4); // retain four decimal places } // Another rounding method has the same principle. // Two parameters: num is the data to be converted. N is the number of digits to be converted. // Cheng (123.456, 2); // retain 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> <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 be retained, rounded to" onclick = "f2 ();"/> <Input type = "button" value = "cheng" onclick = "cheng (123.456, 2);"/> |