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

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 可选

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

返回值

A string representing the specified BigInt 对象。

异常

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

描述

BigInt object overrides the toString() 方法在 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.

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.

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

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 .

范例

使用 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 类型。

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

规范

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

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
toString Chrome 67 Edge 79 Firefox 68 IE No Opera 54 Safari No WebView Android 67 Chrome Android 67 Firefox Android 68 Opera Android 48 Safari iOS No Samsung Internet Android 9.0 nodejs 10.4.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: