BigInt.prototype.toString() - JavaScript 编辑

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

Syntax

bigIntObj.toString([radix])

Parameters

radixOptional
Optional. An integer in the range 2 through 36 specifying the base to use for representing numeric values.

Return value

A string representing the specified BigInt object.

Exceptions

RangeError
If toString() is given a radix less than 2 or greater than 36, a RangeError is thrown.

Description

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

The toString() method parses its first argument, and attempts to return a string representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16) a through f are used.

If the radix is not specified, the preferred radix is assumed to be 10.

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

Examples

Using toString

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

Negative-zero BigInt

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.

(-0n).toString();      // '0'
BigInt(-0).toString(); // '0'

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'BigInt.prototype.toString()' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:135 次

字数:5840

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文