--- title: Number.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/Number/toString tags: - Bug - Number.prototype.toString() - Tips translation_of: Web/JavaScript/Reference/Global_Objects/Number/toString ---
{{JSRef}}
toString()
方法返回指定 {{jsxref("Number")}} 对象的字符串表示形式。
numObj.toString([radix])
radix
参数,则默认值为 10。如果 toString()
的 radix 参数不在 2 到 36 之间,将会抛出一个 {{jsxref("RangeError")}}。
{{jsxref("Number")}} 对象覆盖了 {{jsxref("Object")}} 对象上的 toString()
方法,它不是继承的 {{jsxref("Object.prototype.toString()")}}。对于 {{jsxref( "Number")}} 对象,toString()
方法以指定的基数返回该对象的字符串表示。
如果转换的基数大于10,则会使用字母来表示大于9的数字,比如基数为16的情况,则使用a到f的字母来表示10到15。
如果基数没有指定,则使用 10。
如果对象是负数,则会保留负号。即使radix是2时也是如此:返回的字符串包含一个负号(-)前缀和正数的二进制表示,不是 数值的二进制补码。
进行数字到字符串的转换时,建议用小括号将要转换的目标括起来,防止出错。
var count = 10;
console.log(count.toString()); // 输出 '10'
console.log((17).toString()); // 输出 '17'
console.log((17.2).toString()); // 输出 '17.2'
var x = 6;
console.log(x.toString(2)); // 输出 '110'
console.log((254).toString(16)); // 输出 'fe'
console.log((-10).toString(2)); // 输出 '-1010'
console.log((-0xff).toString(2)); // 输出 '-11111111'
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.1. |
{{SpecName('ES5.1', '#sec-15.7.4.2', 'Number.prototype.tostring')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-number.prototype.tostring', 'Number.prototype.tostring')}} | {{Spec2('ES6')}} |