Home >Web Front-end >JS Tutorial >JavaScript toFixed() method_Basic knowledge

JavaScript toFixed() method_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:29:181372browse
Definition and Usage
The toFixed() method rounds Number to a number with specified decimal places.

Syntax
NumberObject.toFixed(num)num Required. Specifies the number of decimal places, which is a value between 0 and 20, inclusive. Some implementations can support a larger range of values. If this parameter is omitted, 0 will be used instead.

Return value
Returns the string representation of NumberObject, which does not use exponential counting and has a fixed num digits after the decimal point. The number is rounded if necessary and padded with zeros so that it reaches the specified length. If num is greater than le 21, this method simply calls NumberObject.toString(), returning a string in exponential notation.

Throws
A RangeError exception is thrown when num is too small or too large. Values ​​between 0 and 20 do not raise this exception. Some implementations support a larger or smaller range of values.

A TypeError exception is thrown when the object calling this method is not a Number.

Example

Copy code The code is as follows:

< script type="text/javascript">
var num = new Number(13.37);
document.write (num.toFixed(1))


Result: 13.4
Javascript correction to the system’s toFixed() method
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn