blob: 11b62a1532f64498e5ebce8df94c38340f3e57be (
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
|
---
title: 'RangeError: radix must be an integer'
slug: Web/JavaScript/Reference/Errors/Bad_radix
tags:
- Error
- Errors
- JavaScript
- RangeError
translation_of: Web/JavaScript/Reference/Errors/Bad_radix
---
<div>{{jsSidebar("Errors")}}</div>
<p>JavaScript の例外 "radix must be an integer at least 2 and no greater than 36" は、 {{jsxref("Number.prototype.toString()")}} メソッドまたは {{jsxref("BigInt.prototype.toString()")}} メソッドのオプションの <code>radix</code> 引数が指定されたものの、 2 から 36 までの範囲になかったときに発生します。</p>
<h2 id="Message">エラーメッセージ</h2>
<pre class="brush: js">RangeError: invalid argument (Edge)
RangeError: radix must be an integer at least 2 and no greater than 36 (Firefox)
RangeError: toString() radix argument must be between 2 and 36 (Chrome)
</pre>
<h2 id="Error_type">エラーの種類</h2>
<p>{{jsxref("RangeError")}}</p>
<h2 id="What_went_wrong">エラーの原因</h2>
<p>{{jsxref("Number.prototype.toString()")}} メソッドまたは {{jsxref("BigInt.prototype.toString()")}} メソッドのオプションの <code>radix</code> 引数が指定されています。この引数は、数値を表すために使用する基数を指定する 2 から 36 の整数 (数) でなければなりません。例えば、 10 進数の数値 169 は 16 進数では A9 と表現されます。</p>
<p>なぜ上限が 36 なのでしょうか? 10 よりも大きい基数の場合は、アルファベットが数値として用いられます。したがって、 (英語およびほかの多くの言語で用いられる) ラテン文字のアルファベットは 26 文字しかないため、基数は 36 以上にすることができません。</p>
<p>一般的な基数には、次のものがあります。</p>
<ul>
<li><a href="https://ja.wikipedia.org/wiki/%E4%BA%8C%E9%80%B2%E6%B3%95">二進法</a>のための 2、</li>
<li><a href="https://ja.wikipedia.org/wiki/%E5%85%AB%E9%80%B2%E6%B3%95">八進法</a>のための 8、</li>
<li><a href="https://ja.wikipedia.org/wiki/%E5%8D%81%E9%80%B2%E6%B3%95">十進法</a>のための 10、</li>
<li><a href="https://ja.wikipedia.org/wiki/%E5%8D%81%E5%85%AD%E9%80%B2%E6%B3%95">十六進法</a>のための 16。</li>
</ul>
<h2 id="Examples">例</h2>
<h3 id="Invalid_cases">無効な場合</h3>
<pre class="brush: js example-bad">(42).toString(0);
(42).toString(1);
(42).toString(37);
(42).toString(150);
// フォーマットのために、このような文字列は使用できません。
(12071989).toString('MM-dd-yyyy');
</pre>
<h3 id="Valid_cases">有効な場合</h3>
<pre class="brush: js example-good">(42).toString(2); // "101010" (二進法)
(13).toString(8); // "15" (八進法)
(0x42).toString(10); // "66" (十進法)
(100000).toString(16) // "186a0" (十六進法)
</pre>
<h2 id="See_also">関連情報</h2>
<ul>
<li>{{jsxref("Number.prototype.toString()")}}</li>
<li>{{jsxref("BigInt.prototype.toString()")}}</li>
</ul>
|