aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/precision_range/index.html
blob: 08b07dd34f4e61abf8374083f6bf47b70bf4f703 (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: 'RangeError: precision is out of range'
slug: Web/JavaScript/Reference/Errors/Precision_range
tags:
- Error
- Errors
- JavaScript
- RangeError
translation_of: Web/JavaScript/Reference/Errors/Precision_range
---
<div>{{jsSidebar("Errors")}}</div>

<p>JavaScript の例外 "precision is out of range" は、 <code>toFixed</code> または <code>toPrecision</code> に 0 から 20 (または 21) までの範囲外の数値が渡された場合に発生します。</p>
  
<h2 id="Message">エラーメッセージ</h2>

<pre class="brush: js">RangeError: The number of fractional digits is out of range (Edge)
RangeError: The precision is out of range (Edge)
RangeError: precision {0} out of range (Firefox)
RangeError: toExponential() argument must be between 0 and 20 (Chrome)
RangeError: toFixed() digits argument must be between 0 and 20 (Chrome)
RangeError: toPrecision() argument must be between 1 and 21 (Chrome)
</pre>

<h2 id="Error_type">エラーの種類</h2>

<p>{{jsxref("RangeError")}}</p>

<h2 id="What_went_wrong">エラーの原因</h2>

<p>これらのメソッドのいずれかで、 範囲外の精度を引数を使用しています。</p>

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

<p>これらのメソッドで許可されている範囲は、通常 0 と 20(または 21)の間です。しかし、ECMAScript 仕様では、この範囲の拡張が認められています。</p>

<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">メソッド</th>
      <th scope="col">Firefox (SpiderMonkey)</th>
      <th scope="col">Chrome, Opera (V8)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{jsxref("Number.prototype.toExponential()")}}</td>
      <td>0 から 100</td>
      <td>0 から 20</td>
    </tr>
    <tr>
      <td>{{jsxref("Number.prototype.toFixed()")}}</td>
      <td>-20 から 100</td>
      <td>0 から 20</td>
    </tr>
    <tr>
      <td>{{jsxref("Number.prototype.toPrecision()")}}</td>
      <td>1 から 100</td>
      <td>1 から 21</td>
    </tr>
  </tbody>
</table>

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

<h3 id="Invalid_cases">無効なケース</h3>

<pre class="brush: js example-bad">77.1234.toExponential(-1);  // RangeError
77.1234.toExponential(101); // RangeError

2.34.toFixed(-100);         // RangeError
2.34.toFixed(1001);         // RangeError

1234.5.toPrecision(-1);     // RangeError
1234.5.toPrecision(101);    // RangeError
</pre>

<h3 id="Valid_cases">有効な場合</h3>

<pre class="brush: js example-good">77.1234.toExponential(4); // 7.7123e+1
77.1234.toExponential(2); // 7.71e+1

2.34.toFixed(1); // 2.3
2.35.toFixed(1); // 2.4 (この場合は丸めが発生することに注意してください)

5.123456.toPrecision(5); // 5.1235
5.123456.toPrecision(2); // 5.1
5.123456.toPrecision(1); // 5
</pre>

<h2 id="See_also">関連情報</h2>

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