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
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. |
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.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
Number
Number.isFinite()
Number.isInteger()
Number.isNaN()
Number.isSafeInteger()
Number.parseFloat()
Number.parseInt()
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toLocaleString()
Number.prototype.toPrecision()
Number.prototype.toSource()
Number.prototype.toString()
Number.prototype.valueOf()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()