The toString()
method returns a string representing the specified Number
object.
numObj.toString([radix])
radix
A string representing the specified Number
object.
RangeError
toString()
is given a radix not between 2 and 36, a RangeError
is thrown.The Number
object overrides the toString()
method of the 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.
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 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
.
If the numObj
is not a whole number, the 'dot' sign is used to separate the decimal places.
toString
var count = 10; console.log(count.toString()); // displays '10' console.log((17).toString()); // displays '17' console.log((17.2).toString()); // displays '17.2' var 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'
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Number.prototype.tostring' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Number.prototype.tostring' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Number.prototype.tostring' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString