范围错误:基数必须是整数

当指定了 Number.prototype.toString()BigInt.prototype.toString() 方法的可选 radix 参数且该参数不在 2 到 36 之间时,会发生 JavaScript 异常 "基数必须是至少 2 且不大于 36 的整数"。

¥The JavaScript exception "radix must be an integer at least 2 and no greater than 36" occurs when the optional radix parameter of the Number.prototype.toString() or the BigInt.prototype.toString() method was specified and is not between 2 and 36.

信息

¥Message

RangeError: toString() radix argument must be between 2 and 36 (V8-based & Safari)
RangeError: radix must be an integer at least 2 and no greater than 36 (Firefox)

错误类型

¥Error type

RangeError

什么地方出了错?

¥What went wrong?

指定了 Number.prototype.toString()BigInt.prototype.toString() 方法的可选 radix 参数。它的值必须是 2 到 36 之间的整数(数字),指定用于表示数值的数字系统的基数。例如,十进制(以 10 为基数)数字 169 以十六进制(以 16 为基数)表示为 A9。

¥The optional radix parameter of the Number.prototype.toString() or the BigInt.prototype.toString() method was specified. Its value must be an integer (a number) between 2 and 36, specifying the base of the number system to be used for representing numeric values. For example, the decimal (base 10) number 169 is represented in hexadecimal (base 16) as A9.

为什么这个参数的值限制为 36?大于 10 的基数使用字母字符作为数字;因此,基数不能大于 36,因为拉丁字母(英语和许多其他语言使用的)只有 26 个字符。

¥Why is this parameter's value limited to 36? A radix that is larger than 10 uses alphabetical characters as digits; therefore, the radix can't be larger than 36, since the Latin alphabet (used by English and many other languages) only has 26 characters.

最常见的基数:

¥The most common radixes:

示例

¥Examples

无效案例

¥Invalid cases

js
(42).toString(0);
(42).toString(1);
(42).toString(37);
(42).toString(150);
// You cannot use a string like this for formatting:
(12071989).toString("MM-dd-yyyy");

有效案例

¥Valid cases

js
(42).toString(2); // "101010" (binary)
(13).toString(8); // "15" (octal)
(0x42).toString(10); // "66" (decimal)
(100000).toString(16); // "186a0" (hexadecimal)

也可以看看