BigInt.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

BigInt 值的 toString() 方法返回表示指定 BigInt 值的字符串。结尾的 "n" 不是字符串的一部分。

¥The toString() method of BigInt values returns a string representing the specified BigInt value. The trailing "n" is not part of the string.

Try it

语法

¥Syntax

js
toString()
toString(radix)

参数

¥Parameters

radix Optional

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

返回值

¥Return value

表示指定 BigInt 值的字符串。

¥A string representing the specified BigInt value.

例外情况

¥Exceptions

RangeError

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

描述

¥Description

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

¥The BigInt object overrides the toString method of Object; it does not inherit Object.prototype.toString(). For BigInt 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.

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

¥If the specified BigInt 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 BigInt value preceded by a - sign, not the two's complement of the BigInt value.

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

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

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

¥Because BigInt doesn't have a [Symbol.toPrimitive]() method, JavaScript calls the toString() method automatically when a BigInt object is used in a context expecting a string, such as in a template literal. However, BigInt 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
BigInt.prototype.toString = () => "Overridden";
console.log(`${1n}`); // "1"
console.log(`${Object(1n)}`); // "Overridden"

示例

¥Examples

使用 toString()

¥Using toString()

js
17n.toString(); // "17"
66n.toString(2); // "1000010"
254n.toString(16); // "fe"
(-10n).toString(2); // "-1010"
(-0xffn).toString(2); // "-11111111"

负零 BigInt

¥Negative-zero BigInt

不存在负零 BigInt,因为整数中没有负零。-0.0 是 IEEE 浮点概念,仅出现在 JavaScript Number 类型中。

¥There is no negative-zero BigInt as there are no negative zeros in integers. -0.0 is an IEEE floating-point concept that only appears in the JavaScript Number type.

js
(-0n).toString(); // "0"
BigInt(-0).toString(); // "0"

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看