Number.prototype.toFixed()

Number 值的 toFixed() 方法使用 定点表示法 格式化该数字。

¥The toFixed() method of Number values formats this number using fixed-point notation.

Try it

语法

¥Syntax

js
toFixed()
toFixed(digits)

参数

¥Parameters

digits Optional

小数点后出现的位数;应该是介于 0100 之间的值(包含 0100)。如果省略该参数,则将其视为 0

返回值

¥Return value

使用定点表示法表示给定数字的字符串。

¥A string representing the given number using fixed-point notation.

例外情况

¥Exceptions

RangeError

如果 digits 不在 0100(含)之间,则抛出该异常。

TypeError

如果在不是 Number 的对象上调用此方法,则会抛出该异常。

描述

¥Description

toFixed() 方法返回数字的字符串表示形式,不使用 指数表示法,并且小数点后恰好有 digits 位。如果需要,该数字将被舍入,并且如果需要,小数部分将用零填充,以使其具有指定的长度。

¥The toFixed() method returns a string representation of a number without using exponential notation and with exactly digits digits after the decimal point. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

如果数字的绝对值大于或等于 1021,则该方法使用与 Number.prototype.toString() 相同的算法,并返回指数形式的字符串。如果数字的值非有限,则 toFixed() 返回 "Infinity""NaN""-Infinity"

¥If the absolute value of the number is greater or equal to 1021, this method uses the same algorithm as Number.prototype.toString() and returns a string in exponential notation. toFixed() returns "Infinity", "NaN", or "-Infinity" if the value of the number is non-finite.

对于某些值,toFixed() 的输出可能比 toString() 更精确,因为 toString() 只打印足够的有效数字来区分该数字与相邻的数字值。例如:

¥The output of toFixed() may be more precise than toString() for some values, because toString() only prints enough significant digits to distinguish the number from adjacent number values. For example:

js
(1000000000000000128).toString(); // '1000000000000000100'
(1000000000000000128).toFixed(0); // '1000000000000000128'

但是,选择太高的 digits 精度可能会返回意外结果,因为十进制小数无法用浮点精确表示。例如:

¥However, choosing a digits precision that's too high can return unexpected results, because decimal fractional numbers cannot be represented precisely in floating point. For example:

js
(0.3).toFixed(50); // '0.29999999999999998889776975374843459576368331909180'

示例

¥Examples

使用 toFixed()

¥Using toFixed()

js
const numObj = 12345.6789;

numObj.toFixed(); // '12346'; rounding, no fractional part
numObj.toFixed(1); // '12345.7'; it rounds up
numObj.toFixed(6); // '12345.678900'; additional zeros
(1.23e20).toFixed(2); // '123000000000000000000.00'
(1.23e-10).toFixed(2); // '0.00'
(2.34).toFixed(1); // '2.3'
(2.35).toFixed(1); // '2.4'; it rounds up
(2.55).toFixed(1); // '2.5'
// it rounds down as it can't be represented exactly by a float and the
// closest representable float is lower
(2.449999999999999999).toFixed(1); // '2.5'
// it rounds up as it's less than Number.EPSILON away from 2.45.
// This literal actually encodes the same number value as 2.45

(6.02 * 10 ** 23).toFixed(50); // 6.019999999999999e+23; large numbers still use exponential notation

将 toFixed() 与负数一起使用

¥Using toFixed() with negative numbers

因为成员访问的 precedence 比一元减号高,所以需要对负数表达式进行分组才能得到字符串。

¥Because member access has higher precedence than unary minus, you need to group the negative number expression to get a string.

js
-2.34.toFixed(1); // -2.3, a number
(-2.34).toFixed(1); // '-2.3'

规范

Specification
ECMAScript Language Specification
# sec-number.prototype.tofixed

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看