Number.prototype.toString()

Number 值的 toString() 方法返回表示该数字值的字符串。

¥The toString() method of Number values returns a string representing this number value.

Try it

语法

¥Syntax

js
toString()
toString(radix)

参数

¥Parameters

radix Optional

236 范围内的整数,指定用于表示数值的基数。默认为 10。

返回值

¥Return value

表示指定数值的字符串。如果基数为 10,且数字的量级(忽略符号)大于或等于 1021 或小于 10-6,则使用科学计数法。

¥A string representing the specified number value. Scientific notation is used if radix is 10 and the number's magnitude (ignoring sign) is greater than or equal to 1021 or less than 10-6.

例外情况

¥Exceptions

RangeError

如果 radix 小于 2 或大于 36,则抛出。

TypeError

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

描述

¥Description

Number 对象重写了 ObjecttoString 方法;它不继承 Object.prototype.toString()。对于 Number 值,toString 方法返回指定基数中的值的字符串表示形式。

¥The Number object overrides the toString method of Object; it does not inherit Object.prototype.toString(). For Number values, the toString method returns a string representation of the value in the specified radix.

对于 10 以上的基数,字母表中的字母表示大于 9 的数字。例如,对于十六进制数(以 16 为基数),使用 af

¥For radixes above 10, the letters of the alphabet indicate digits greater than 9. For example, for hexadecimal numbers (base 16) a through f are used.

如果指定的数值为负,则保留符号。即使基数是 2 也是如此;返回的字符串是数字值的正二进制表示形式,前面带有 - 符号,而不是数字值的二进制补码。

¥If the specified number value is negative, the sign is preserved. This is the case even if the radix is 2; the string returned is the positive binary representation of the number value preceded by a - sign, not the two's complement of the number value.

0-0 都以 "0" 作为其字符串表示形式。Infinity 返回 "Infinity"NaN 返回 "NaN"

¥Both 0 and -0 have "0" as their string representation. Infinity returns "Infinity" and NaN returns "NaN".

如果数字不是整数,则使用小数点 . 来分隔小数位。如果基数为 10 并且数字的大小(忽略符号)大于或等于 1021 或小于 10-6,则使用 科学计数法。在这种情况下,返回的字符串始终显式指定指数的符号。

¥If the number is not a whole number, the decimal point . is used to separate the decimal places. Scientific notation is used if the radix is 10 and the number's magnitude (ignoring sign) is greater than or equal to 1021 or less than 10-6. In this case, the returned string always explicitly specifies the sign of the exponent.

js
console.log((10 ** 21.5).toString()); // "3.1622776601683794e+21"
console.log((10 ** 21.5).toString(8)); // "526665530627250154000000"

浮点数的底层表示是以 2 为基数的科学计数法(参见 数字编码)。但是,toString() 方法并不直接使用这个最精确的数字值表示。相反,该算法使用最少的有效数字来区分输出和相邻的数字值。例如,如果数字很大,则同一浮点数将有许多等效的字符串表示形式,而 toString() 将选择右侧 0 最多的字符串表示形式(对于任何给定的基数)。

¥The underlying representation for floating point numbers is base-2 scientific notation (see number encoding). However, the toString() method doesn't directly use this most precise representation of the number value. Rather, the algorithm uses the least number of significant figures necessary to distinguish the output from adjacent number values. For example, if the number is large, there will be many equivalent string representations of the same floating point number, and toString() will choose the one with the most 0s to the right (for any given radix).

js
console.log((1000000000000000128).toString()); // "1000000000000000100"
console.log(1000000000000000100 === 1000000000000000128); // true

另一方面,Number.prototype.toFixed()Number.prototype.toPrecision() 允许你指定精度,并且可以比 toString() 更精确。

¥On the other hand, Number.prototype.toFixed() and Number.prototype.toPrecision() allow you to specify the precision and can be more precise than toString().

toString() 方法要求其 this 值是 Number 基元或封装对象。它为其他 this 值抛出 TypeError,而不尝试将它们强制为数字值。

¥The toString() method requires its this value to be a Number primitive or wrapper object. It throws a TypeError for other this values without attempting to coerce them to number values.

由于 Number 没有 [Symbol.toPrimitive]() 方法,因此当在需要字符串的上下文中(例如在 模板文字 中)使用 Number 对象时,JavaScript 会自动调用 toString() 方法。但是,Number 基元值不会参考 toString() 方法转换为 强制为字符串,而是使用与初始 toString() 实现相同的算法直接转换它们。

¥Because Number doesn't have a [Symbol.toPrimitive]() method, JavaScript calls the toString() method automatically when a Number object is used in a context expecting a string, such as in a template literal. However, Number primitive values do not consult the toString() method to be coerced to strings — rather, they are directly converted using the same algorithm as the initial toString() implementation.

js
Number.prototype.toString = () => "Overridden";
console.log(`${1}`); // "1"
console.log(`${new Number(1)}`); // "Overridden"

示例

¥Examples

使用 toString()

¥Using toString()

js
const count = 10;
console.log(count.toString()); // "10"

console.log((17).toString()); // "17"
console.log((17.2).toString()); // "17.2"

const x = 6;
console.log(x.toString(2)); // "110"
console.log((254).toString(16)); // "fe"
console.log((-10).toString(2)); // "-1010"
console.log((-0xff).toString(2)); // "-11111111"

转换数字字符串的基数

¥Converting radix of number strings

如果你有一个表示非十进制基数的数字的字符串,则可以使用 parseInt()toString() 将其转换为不同的基数。

¥If you have a string representing a number in a non-decimal radix, you can use parseInt() and toString() to convert it to a different radix.

js
const hex = "CAFEBABE";
const bin = parseInt(hex, 16).toString(2); // "11001010111111101011101010111110"

注意精度损失:如果原始数字字符串太大(例如大于 Number.MAX_SAFE_INTEGER),则应使用 BigInt 代替。但是,BigInt 构造函数仅支持表示数字文字的字符串(即以 0b0o0x 开头的字符串)。如果你的原始基数不是二进制、八进制、十进制或十六进制之一,你可能需要手写基数转换器或使用库。

¥Beware of loss of precision: if the original number string is too large (larger than Number.MAX_SAFE_INTEGER, for example), you should use a BigInt instead. However, the BigInt constructor only has support for strings representing number literals (i.e. strings starting with 0b, 0o, 0x). In case your original radix is not one of binary, octal, decimal, or hexadecimal, you may need to hand-write your radix converter, or use a library.

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看