BigInt.prototype.toString()
toString()
方法返回一个字符串,表示指定 BigInt
对象。 后面的 "n" 不是字符串的一部分。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
bigIntObj.toString([radix])
参数
radix
可选- 可选,介于 2 到 36 之间的整数,指定用于表示数值的基数。
返回值
表示指定 BigInt
对象的字符串。
异常
RangeError
- 如果
toString()
的基数小于 2 或大于 36, 则抛出RangeError
。
描述
BigInt
对象重写 Object
对象的 toString()
方法;它不继承 Object.prototype.toString()
。对于 BigInt
对象,toString()
方法返回指定基数中对象的字符串表示形式。
toString()
方法解析其第一个参数,并尝试返回指定基数(base)的字符串表示形式。对于大于 10 的参数,使用字母表中的字母表示大于 9 的数字。例如,对于十六进制数(以16为基数),使用 a 到 f。
如果未指定基数,则假定首选基数为10。
如果 bigIntObj
为负,则保留符号。即使基数是 2,情况也是如此;返回的字符串是 bigIntObj
的正二进制表示,前面是一个 -
符号,而不是 bigIntObj
的两个补码。
例子
Using toString
17n.toString(); // '17'
66n.toString(2); // '1000010'
254n.toString(16); // 'fe'
-10n.toString(2); // -1010'
-0xffn.toString(2); // '-11111111'
Negative-zero BigInt
没有负零 BigInt
,因为整数中没有负零。-0.0
是一个 IEEE 浮点概念,只出现在JavaScript Number
类型中。
(-0n).toString(); // '0'
BigInt(-0).toString(); // '0'
标准
Specification | Status |
---|---|
ECMAScript (ECMA-262) BigInt.prototype.toString() |
Living Standard |
浏览器支持
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.