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

表示指定数值的字符串。

¥A string representing the specified number value.

例外情况

¥Exceptions

RangeError

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

描述

¥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"

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

¥Because Number doesn't have a [@@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

也可以看看