aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html149
1 files changed, 149 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html
new file mode 100644
index 0000000000..30fdc17406
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html
@@ -0,0 +1,149 @@
+---
+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" name="Syntax">语法</h2>
+
+<pre><code><var>numObj</var>.toString([<var>radix</var>])</code>
+</pre>
+
+<h3 id="Parameter" name="Parameter">参数</h3>
+
+<dl>
+ <dt>radix</dt>
+ <dd>指定要用于数字到字符串的转换的基数(从2到36)。如果未指定 <samp>radix</samp> 参数,则默认值为 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" name="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" name="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>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Number.prototype.toFixed()")}}</li>
+ <li>{{jsxref("Number.prototype.toExponential()")}}</li>
+ <li>{{jsxref("Number.prototype.toPrecision()")}}</li>
+</ul>