Definition and usage
The toFixed() method rounds Number into a number with a specified decimal number.
grammar
NumberObject.toFixed(num) Parameter Description
num Required. The number of digits specified in the decimal is a value between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this parameter is omitted, 0 will be replaced.
Return value
Returns the string representation of NumberObject, without exponential counting, there are fixed num digits after the decimal point. If necessary, the number will be rounded and can also be filled with 0 so that it reaches the specified length. If num is greater than le+21, the method only calls NumberObject.toString() and returns a string represented by exponential counting.
Throw out
An exception RangeError is thrown when num is too small or too large. Values between 0 and 20 will not raise this exception. Some implementations support values in a larger or smaller range.
A TypeError exception is thrown when the object calling the method is not Number.
Example
In this example, we will round the number to a number with only one decimal:
Show the number 13.37 with one decimal:<script type="text/javascript">var num = new Number(13.37);document.write (num.toFixed(1))</script>
Output:
Show the number 13.37 with one decimal:13.4
The detailed explanation of the toFixed() method of Number object in JavaScript is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.