toString() method returns a string representing the specified Number 对象。

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.

句法

numObj.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 Number 对象。

异常

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

描述

Number object overrides the toString() 方法在 Object object. (It does not inherit Object.prototype.toString() ). For Number 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 radices 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 .

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

numObj is not a whole number, the 'dot' sign is used to separate the decimal places.

范例

使用 toString

let count = 10
console.log(count.toString())    // displays '10'
console.log((17).toString())     // displays '17'
console.log((17.2).toString())   // displays '17.2'
let x = 6
console.log(x.toString(2))       // displays '110'
console.log((254).toString(16))  // displays 'fe'
console.log((-10).toString(2))   // displays '-1010'
console.log((-0xff).toString(2)) // displays '-11111111'
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Number.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 1 Edge 12 Firefox 1 IE 3 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: