RangeError: radix must be an integer - JavaScript 编辑
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: 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)
Error type
What went wrong?
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.
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:
- 2 for binary numbers,
- 8 for octal numbers,
- 10 for decimal numbers,
- 16 for hexadecimal numbers.
Examples
Invalid cases
(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
(42).toString(2); // "101010" (binary)
(13).toString(8); // "15" (octal)
(0x42).toString(10); // "66" (decimal)
(100000).toString(16) // "186a0" (hexadecimal)
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论