aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html
blob: bafb63339860096aea054726f1188760093a70c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
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
---
<p>{{JSRef}}</p>

<p><strong><code>toString()</code></strong> 方法返回指定 {{jsxref("Number")}} 对象的字符串表示形式。</p>

<h2 id="Syntax">语法</h2>

<pre><code><var>numObj</var>.toString([<var>radix</var>])</code>
</pre>

<h3 id="Parameter">参数</h3>

<dl>
 <dt>radix</dt>
 <dd>指定要用于数字到字符串的转换的基数(从2到36)。如果未指定 <code>radix</code> 参数,则默认值为 10。</dd>
</dl>

<h3 id="异常信息">异常信息</h3>

<dl>
 <dt>{{jsxref("RangeError")}}</dt>
 <dd>
 <p>如果 <code>toString()</code> 的 radix 参数不在 2 到 36 之间,将会抛出一个 {{jsxref("RangeError")}}</p>
 </dd>
</dl>

<h2 id="Description">描述</h2>

<p>{{jsxref("Number")}} 对象覆盖了 {{jsxref("Object")}} 对象上的 <code>toString()</code> 方法,它不是继承的 {{jsxref("Object.prototype.toString()")}}。对于 {{jsxref( "Number")}} 对象,<code>toString()</code> 方法以指定的基数返回该对象的字符串表示。</p>

<p>如果转换的基数大于10,则会使用字母来表示大于9的数字,比如基数为16的情况,则使用a到f的字母来表示10到15。</p>

<p>如果基数没有指定,则使用 10。</p>

<p>如果对象是负数,则会保留负号。即使radix是2时也是如此:返回的字符串包含一个负号(-)前缀和正数的二进制表示,<strong>不是</strong> 数值的二进制补码。</p>

<p>进行数字到字符串的转换时,建议<strong>用小括号将要转换的目标括起来</strong>,防止出错。</p>

<h2 id="Examples">例子</h2>

<pre class="brush: js"><code>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'</code>
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.7.4.2', 'Number.prototype.tostring')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-number.prototype.tostring', 'Number.prototype.tostring')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Number.prototype.toFixed()")}}</li>
 <li>{{jsxref("Number.prototype.toExponential()")}}</li>
 <li>{{jsxref("Number.prototype.toPrecision()")}}</li>
</ul>