diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/global_objects/number | |
parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip |
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/number')
6 files changed, 767 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/index.html new file mode 100644 index 0000000000..8a023d1603 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/index.html @@ -0,0 +1,219 @@ +--- +title: Number +slug: Web/JavaScript/Reference/Global_Objects/Number +tags: + - JavaScript + - NeedsTranslation + - Number + - Reference + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number</code></strong> JavaScript 物件是允許你操作數值的包覆物件. <code>Number</code> 物件是以 <code>Number()</code> 建構子來建立的。</p> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox">new Number(value);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>value</code></dt> + <dd>用來建立物件的數值。</dd> +</dl> + +<h2 id="說明">說明</h2> + +<p><code>Number</code> 物件主要的用途:</p> + +<ul> + <li>如果參數沒辦法被轉換成數字,則它會回傳 {{jsxref("NaN")}} 。</li> + <li>在不是使用建構式的情境中(即不用 {{jsxref("Operators/new", "new")}} 運算子), <code>Number</code> 可以被用來轉換型別.</li> +</ul> + +<h2 id="屬性">屬性</h2> + +<dl> + <dt>{{jsxref("Number.EPSILON")}}</dt> + <dd>介於1和大於1的最小值之可表示的差。</dd> + <dt>{{jsxref("Number.MAX_SAFE_INTEGER")}}</dt> + <dd>JavaScript 中 IEEE-754 雙精度範圍間的最大整數 (<code>2<sup>53</sup> - 1</code>) 。</dd> + <dt>{{jsxref("Number.MAX_VALUE")}}</dt> + <dd>可表示的最大正整數。</dd> + <dt>{{jsxref("Number.MIN_SAFE_INTEGER")}}</dt> + <dd>JavaScript 中 IEEE-754 雙精度範圍間的最小整數 (<code>-(2<sup>53</sup> - 1)</code>) 。</dd> + <dt>{{jsxref("Number.MIN_VALUE")}}</dt> + <dd>可表示的最小值,即最靠近0的正整數?(<code><math><semantics><mrow><mn>5.00</mn><mo>×</mo><msup><mn>10</mn><mn>324</mn></msup></mrow><annotation encoding="TeX">5.00\times10^{324}</annotation></semantics></math></code>)。</dd> + <dt>{{jsxref("Number.NaN")}}</dt> + <dd>特別用來表示<strong>非數值</strong>的物件。</dd> + <dt>{{jsxref("Number.NEGATIVE_INFINITY")}}</dt> + <dd>特別用來表示<strong>負無窮</strong>的數值。</dd> + <dt>{{jsxref("Number.POSITIVE_INFINITY")}}</dt> + <dd>特別用來表示<strong>正無窮</strong>的數值。</dd> + <dt>{{jsxref("Number.prototype")}}</dt> + <dd>允許被添加到 <code>Number</code> 物件的屬性。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{jsxref("Number.isNaN()")}}</dt> + <dd>判斷傳入的值是不是 NaN.</dd> + <dt>{{jsxref("Number.isFinite()")}}</dt> + <dd>判斷傳入的值是不是一個有限的數值。</dd> + <dt>{{jsxref("Number.isInteger()")}}</dt> + <dd>判斷傳入的值是不是一個整數。</dd> + <dt>{{jsxref("Number.isSafeInteger()")}}</dt> + <dd>判斷傳入的值是不是在 IEEE-754 雙精度範圍間 (即介於 <code>-(2<sup>53</sup> - 1)</code> 和 <code>2<sup>53</sup> - 1</code>之前)。</dd> + <dt><s class="obsoleteElement">{{jsxref("Number.toInteger()")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">被用來評估傳入的值且將其轉換成整數 (或 {{jsxref("Global_Objects/Infinity", "Infinity")}}) 但已被移除。</s></dd> + <dt>{{jsxref("Number.parseFloat()")}}</dt> + <dd>這個方法和全域物件的 {{jsxref("parseFloat", "parseFloat()")}} 相同。</dd> + <dt>{{jsxref("Number.parseInt()")}}</dt> + <dd>這個方法和全域物件的 {{jsxref("parseInt", "parseInt()")}} 相同。</dd> +</dl> + +<h2 id="Number_實體"><code>Number</code> 實體</h2> + +<p>所有 <code>Number</code> 實體都會繼承其建構式的 {{jsxref("Number.prototype")}}。<code>Number</code> 的原型物件可以被修改並作用在所有 <code>Number</code> 實體。</p> + +<h3 id="方法_2">方法</h3> + +<div>{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Number/prototype', '方法')}}</div> + +<h2 id="範例">範例</h2> + +<h3 id="使用_Number_物件去指派值給數值變數">使用 <code>Number</code> 物件去指派值給數值變數</h3> + +<p>下列的範例使用 <code>Number</code> 物件的屬性去指派值給數個數值變數:</p> + +<pre class="brush: js">var biggestNum = Number.MAX_VALUE; +var smallestNum = Number.MIN_VALUE; +var infiniteNum = Number.POSITIVE_INFINITY; +var negInfiniteNum = Number.NEGATIVE_INFINITY; +var notANum = Number.NaN; +</pre> + +<h3 id="Number_的整數範圍"><code>Number</code> 的整數範圍</h3> + +<p>下面的範例展示了最小和最大的整數,其可以被表示成 <code>Number</code> 物件(細節請參考 ECMAScript standard, chapter <em>8.5 The Number Type</em>):</p> + +<pre class="brush: js">var biggestInt = 9007199254740992; +var smallestInt = -9007199254740992; +</pre> + +<p>當在解析已經被序列化的 JSON 的資料時,填入這個範圍之外的整數並且 JSON 剖析器強制將其轉成 <code>Number</code> 型別造成損壞是可預期的。將範圍之外的正數換成以 {{jsxref("String")}} 表示反倒是一個可行的替代方案。</p> + +<h3 id="使用_Number_轉換_Date_物件為_Unix_時間戳記">使用 <code>Number</code> 轉換 <code>Date</code> 物件為 Unix 時間戳記</h3> + +<p>下面的範例將 <code>Number</code> 視為函式,並且使用它將 {{jsxref("Date")}} 轉換成時間戳記:</p> + +<pre class="brush: js">var d = new Date('December 17, 1995 03:24:00'); +console.log(Number(d)); // 819199440000 + +</pre> + +<h3 id="轉換數值字串成數值">轉換數值字串成數值</h3> + +<pre class="brush: js">Number("123") // 123 +Number("12.3") // 12.3 +Number("") // 0 +Number("0x11") // 17 +Number("0b11") // 3 +Number("0o11") // 9 +Number("foo") // NaN +Number("100a") // NaN +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註記</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', 'Number')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number-objects', 'Number')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>New methods and properties added: {{jsxref("Number.EPSILON", "EPSILON")}}, {{jsxref("Number.isFinite", "isFinite")}}, {{jsxref("Number.isInteger", "isInteger")}}, {{jsxref("Number.isNaN", "isNaN")}}, {{jsxref("Number.parseFloat", "parseFloat")}}, {{jsxref("Number.parseInt", "parseInt")}}</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number-objects', 'Number')}}</td> + <td>{{Spec2('ESDraft')}}</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("NaN")}}</li> + <li>The {{jsxref("Math")}} global object</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html new file mode 100644 index 0000000000..a15d29c7c0 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html @@ -0,0 +1,93 @@ +--- +title: Number.isFinite() +slug: Web/JavaScript/Reference/Global_Objects/Number/isFinite +tags: + - JavaScript + - Method + - Number + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isFinite +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.isFinite()</code></strong> 方法會判斷傳入的值是否為有限數(finite number)。</p> + +<div>{{EmbedInteractiveExample("pages/js/number-isfinite.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox">Number.isFinite(v<var>alue</var>)</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>value</code></dt> + <dd>The value to be tested for finiteness.</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p>A {{jsxref("Boolean")}} indicating whether or not the given value is a finite number.</p> + +<h2 id="說明">說明</h2> + +<p>In comparison to the global {{jsxref("isFinite", "isFinite()")}} function, this method doesn't forcibly convert the parameter to a number. This means only values of the type number, that are also finite, return <code>true</code>.</p> + +<h2 id="範例">範例</h2> + +<pre class="brush: js">Number.isFinite(Infinity); // false +Number.isFinite(NaN); // false +Number.isFinite(-Infinity); // false + +Number.isFinite(0); // true +Number.isFinite(2e64); // true + +Number.isFinite('0'); // false, would've been true with + // global isFinite('0') +Number.isFinite(null); // false, would've been true with + // global isFinite(null) +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<pre class="brush: js">Number.isFinite = Number.isFinite || function(value) { + return typeof value === 'number' && isFinite(value); +} +</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('ES6', '#sec-number.isfinite', 'Number.isInteger')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.isfinite', 'Number.isInteger')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Number.isFinite")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>The {{jsxref("Number")}} object it belongs to.</li> + <li>The global function {{jsxref("isFinite")}}.</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html new file mode 100644 index 0000000000..9209e40779 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html @@ -0,0 +1,99 @@ +--- +title: Number.isNaN() +slug: Web/JavaScript/Reference/Global_Objects/Number/isNaN +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isNaN +--- +<div>{{JSRef}}</div> + +<p>The <strong><code>Number.isNaN()</code></strong> method determines whether the passed value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}. It is a more robust version of the original, global {{jsxref("isNaN", "isNaN()")}}.</p> + +<div>{{EmbedInteractiveExample("pages/js/number-isnan.html", "taller")}}</div> + + + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code>Number.isNaN(<var>value</var>)</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>value</code></dt> + <dd>The value to be tested for {{jsxref("NaN")}}.</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<p><strong>true</strong> if the given value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}; otherwise, <strong>false</strong>.</p> + +<h2 id="Description">Description</h2> + +<p>Due to both equality operators, {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} and {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}, evaluating to <code>false</code> when checking if {{jsxref("NaN")}} <em>is</em> {{jsxref("NaN")}}, the function <code>Number.isNaN()</code> has become necessary. This situation is unlike all other possible value comparisons in JavaScript.</p> + +<p>In comparison to the global {{jsxref("isNaN", "isNaN()")}} function, <code>Number.isNaN()</code> doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to {{jsxref("NaN")}}, but aren't actually the same value as {{jsxref("NaN")}}. This also means that only values of the type number, that are also {{jsxref("NaN")}}, return <code>true</code>.</p> + +<h2 id="Examples">Examples</h2> + +<pre class="brush: js">Number.isNaN(NaN); // true +Number.isNaN(Number.NaN); // true +Number.isNaN(0 / 0); // true + +// e.g. these would have been true with global isNaN() +Number.isNaN('NaN'); // false +Number.isNaN(undefined); // false +Number.isNaN({}); // false +Number.isNaN('blabla'); // false + +// These all return false +Number.isNaN(true); +Number.isNaN(null); +Number.isNaN(37); +Number.isNaN('37'); +Number.isNaN('37.37'); +Number.isNaN(''); +Number.isNaN(' '); +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>The following works because NaN is the only value in javascript which is not equal to itself.</p> + +<pre class="brush: js">Number.isNaN = Number.isNaN || function(value) { + return value !== value; +} +</pre> + +<h2 id="Specifications">Specifications</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('ES2015', '#sec-number.isnan', 'Number.isnan')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Number.isNaN")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Number")}}</li> + <li>{{jsxref("isNaN", "isNaN()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html new file mode 100644 index 0000000000..2a41977a2d --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html @@ -0,0 +1,84 @@ +--- +title: Number.prototype +slug: Web/JavaScript/Reference/Global_Objects/Number/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.prototype</code></strong> 屬性用來表示 {{jsxref("Number")}} 建構式的原型。</p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="說明">說明</h2> + +<p>所有 {{jsxref("Number")}} 實體都繼承自 <code>Number.prototype</code> 。{{jsxref("Number")}} 建構式的原型物件可以被修改並作用在所有 {{jsxref("Number")}} 實體。</p> + +<h2 id="屬性">屬性</h2> + +<dl> + <dt><code>Number.prototype.constructor</code></dt> + <dd>回傳建立這個物件實體的建構式。預設為 {{jsxref("Number")}} 物件。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{jsxref("Number.prototype.toExponential()")}}</dt> + <dd>回傳以「科學記數法」表示的數值字串。</dd> + <dt>{{jsxref("Number.prototype.toFixed()")}}</dt> + <dd>回傳以定點表示的數值字串。</dd> + <dt>{{jsxref("Number.prototype.toLocaleString()")}}</dt> + <dd>回傳以當地語言為主的數值字串。這覆寫 {{jsxref("Object.prototype.toLocaleString()")}} 的方法。</dd> + <dt>{{jsxref("Number.prototype.toPrecision()")}}</dt> + <dd>回傳以定點或科學記數表示的數值字串。</dd> + <dt>{{jsxref("Number.prototype.toSource()")}} {{non-standard_inline}}</dt> + <dd>Returns an object literal representing the specified {{jsxref("Number")}} object; you can use this value to create a new object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.</dd> + <dt>{{jsxref("Number.prototype.toString()")}}</dt> + <dd>回傳以特定基數表示的數值字串。這覆寫 {{jsxref("Object.prototype.toString()")}} 的方法 。</dd> + <dt>{{jsxref("Number.prototype.valueOf()")}}</dt> + <dd>回傳這個物件的原始型別,即原始數值。這覆寫 {{jsxref("Object.prototype.valueOf()")}} 。</dd> +</dl> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註記</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', 'Number')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-properties-of-the-number-prototype-object', 'Number')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-properties-of-the-number-prototype-object', 'Number')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Number.prototype")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("Number")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html new file mode 100644 index 0000000000..622314ecfb --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html @@ -0,0 +1,164 @@ +--- +title: Number.prototype.toExponential() +slug: Web/JavaScript/Reference/Global_Objects/Number/toExponential +tags: + - 數字 + - 科學技數法 +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toExponential +--- +<div>{{JSRef}}</div> + +<p><strong><code>toExponential()</code></strong> method 用來將數字轉成「科學記數法」格式。</p> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><code><var>numObj</var>.toExponential([<var>fractionDigits</var>])</code></pre> + +<h3 id="參數">參數</h3> + +<table class="standard-table"> + <thead> + <tr> + <th>參數</th> + <th>可選</th> + <th>默認值</th> + <th>類型</th> + <th>說明</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>fractionDigits</code></td> + <td>●</td> + <td><font face="Consolas, Liberation Mono, Courier, monospace">默認盡可能將數字完整顯示</font></td> + <td><code>{{jsxref("Number")}}(正整數)</code></td> + <td>小數點後的位數。需為 0 至 20 之間。</td> + </tr> + </tbody> +</table> + +<h3 id="回傳直">回傳直</h3> + +<p>一 string,將數字以「科學計數法」格式表示出來</p> + +<h3 id="Exceptions">Exceptions</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd><code>若 fractionDigits</code> 超出範圍(可接受的範圍是 0 ~ 20 之間的正整數)觸發 {{jsxref("RangeError")}}。</dd> + <dt>{{jsxref("TypeError")}}</dt> + <dd>若參數 <code>fractionDigits</code> 不是 {{jsxref("Number")}},則觸發{{jsxref("TypeError")}}。</dd> +</dl> + +<h2 id="Description">Description</h2> + +<p>If the <code>fractionDigits</code> argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.</p> + +<p>If you use the <code>toExponential()</code> method for a numeric literal and the numeric literal has no exponent and no decimal point, leave whitespace(s) before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.</p> + +<p>If a number has more digits than requested by the <code>fractionDigits</code> parameter, the number is rounded to the nearest number represented by <code>fractionDigits</code> digits. See the discussion of rounding in the description of the {{jsxref("Number.prototype.toFixed", "toFixed()")}} method, which also applies to <code>toExponential()</code>.</p> + +<h2 id="範例">範例</h2> + +<h3 id="Using_toExponential">Using <code>toExponential</code></h3> + +<pre class="brush: js">var numObj = 77.1234; + +console.log(numObj.toExponential()); // logs 7.71234e+1 +console.log(numObj.toExponential(4)); // logs 7.7123e+1 +console.log(numObj.toExponential(2)); // logs 7.71e+1 +console.log(77.1234.toExponential()); // logs 7.71234e+1 +console.log(77 .toExponential()); // logs 7.7e+1 +</pre> + +<h2 id="Specifications">Specifications</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('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.5.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.6', 'Number.prototype.toExponential')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.toexponential', 'Number.prototype.toExponential')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.prototype.toexponential', 'Number.prototype.toExponential')}}</td> + <td>{{Spec2('ESDraft')}}</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.toPrecision()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html new file mode 100644 index 0000000000..7a5afb608f --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html @@ -0,0 +1,108 @@ +--- +title: Number.prototype.toFixed() +slug: Web/JavaScript/Reference/Global_Objects/Number/toFixed +tags: + - JavaScript + - Method + - Number + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed +--- +<div>{{JSRef}}</div> + +<p><strong><code>toFixed()</code></strong> 方法會使用定點小數表示法(fixed-point notation)來格式化數字。</p> + +<div>{{EmbedInteractiveExample("pages/js/number-tofixed.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox notranslate"><code><var>numObj</var>.toFixed([<var>digits</var>])</code></pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>digits 小數位</code></dt> + <dd>選擇性輸入。顯示數值至多少個小數點,範圍由0到20之間,執行時或可支援非常大範圍的數值。如果無輸入會默認做0。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p>一個代表以定點小數表示法(fixed-point notation)格式化數字後的字串。</p> + +<h3 id="例外">例外</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd>If <code>digits</code> is too small or too large. Values between 0 and 100, inclusive, will not cause a {{jsxref("RangeError")}}. Implementations are allowed to support larger and smaller values as chosen.</dd> + <dt>{{jsxref("TypeError")}}</dt> + <dd>If this method is invoked on an object that is not a {{jsxref( "Number")}}.</dd> +</dl> + +<h2 id="說明">說明</h2> + +<p><strong><code>toFixed()</code></strong> returns a string representation of <code>numObj</code> that does not use exponential notation and has exactly <code>digits</code> digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If <code>numObj</code> is greater than <code>1e+21</code>, this method simply calls {{jsxref("Number.prototype.toString()")}} and returns a string in exponential notation.</p> + +<h2 id="範例">範例</h2> + +<h3 id="Using_toFixed">Using <code>toFixed</code></h3> + +<pre class="brush: js notranslate">var numObj = 12345.6789; + +numObj.toFixed(); // Returns '12346': note rounding, no fractional part +numObj.toFixed(1); // Returns '12345.7': note rounding +numObj.toFixed(6); // Returns '12345.678900': note added zeros +(1.23e+20).toFixed(2); // Returns '123000000000000000000.00' +(1.23e-10).toFixed(2); // Returns '0.00' +2.34.toFixed(1); // Returns '2.3' +2.35.toFixed(1); // Returns '2.4'. Note that it rounds up in this case. +-2.34.toFixed(1); // Returns -2.3 (due to operator precedence, negative number literals don't return a string...) +(-2.34).toFixed(1); // Returns '-2.3' (...unless you use parentheses) +</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('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.5.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.5', 'Number.prototype.toFixed')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Number.toFixed")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> |