toString
Summary
Returns a string representation of a number.
Syntax
toString([ radix ])
- radix
- Optional. The base number system to convert to given as 2 - 36, inclusive. Defaults to 10.
Return Value
String representing the number value in the given base number system.
Negative numbers are simply preceded by the -
sign, i.e. there is no conversion to a system like the Two’s Complement for binary.
Examples
The following example illustrates the use of the toString method with a number.
var mph_number = 234.567;
var mph_string = mph_number.toString();
console.log(mph_string);
// Output: "234.567"
console.log(mph_string.length);
// Output: 7
The following example illustrates the use of the toString method with a radix argument.
var mph_number = 199;
// Convert to hexadecimal
mph_number.toString(16);
// Returns: "c7"
// Convert to decimal
mph_number.toString(10);
// Returns: "199"
// Convert to octal
mph_number.toString(8);
// Returns: "307"
// Convert to binary
mph_number.toString(2);
// Returns: "11000111"
implicit use of toString()
var mph_number = 199;
String(mph_number) === mph_number.toString(10);
(mph_number + "") === mph_number.toString(10);
Remarks
Throws
RangeError
when a radix outside the bounds of 2 - 36 (inclusive) was given.
See also
Other articles
External resources
Specification
15.7.4.2 Number.prototype.toString(radix)
ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
Attributions
Microsoft Developer Network: Article