JavaScript toFixed () method, javascripttofixed
Definition and usage
The toFixed () method rounds a Number to a specified Number of decimal places.
Syntax
NumberObject. toFixed (num)
| Parameters |
Description |
| Num |
Required. Specifies the number of digits in a decimal place, ranging from 0 ~ Values between 20, including 0 and 20. Some implementations support a larger value range. If this parameter is omitted, it is replaced by 0. |
Return Value
Returns the string representation of NumberObject. Without the exponential notation, there is a fixed num digit after the decimal point. If necessary, the number is rounded off, or 0 can be used to make up for the specified length. If num is greater than le + 21, this method only calls NumberObject. toString () and returns the string represented by exponential notation.
Throw
When num is too small or too large, an exception RangeError is thrown. 0 ~ The value between 20 does not cause this exception. Some implementations support values in a larger or smaller range.
If the object that calls this method is not a Number, a TypeError error is thrown.
Instance
In this example, we will round the number to only a decimal number:
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 1 decimal: 13.4