aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html
blob: addec4288b21d24eb87c7b72d18aabe0030db76e (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
102
103
104
105
106
---
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>{{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>