RangeError: precision is out of range

๋ฉ”์‹œ์ง€

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)

์—๋Ÿฌ ํ˜•์‹

RangeError

๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ์„๊นŒ?

์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ๋“ค ์ค‘ ํ•˜๋‚˜์—์„œ,  ์‹คํ–‰ ์ธ์ž(argument)๊ฐ€ ์ •ํ™•๋„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค. :

์ด ๋ฉ”์†Œ๋“œ๋“ค์—๊ฒŒ ํ—ˆ์šฉ๋œ ๋ฒ”์œ„๋Š” 0๋ถ€ํ„ฐ 20 (๋˜๋Š” 21)๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ECMAScript ์ŠคํŽ™์€ ์•„๋ž˜์˜ ๋ฒ”์œ„๊นŒ์ง€ ํ™•์žฅํ•˜์—ฌ ํ—ˆ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Method Firefox (SpiderMonkey) Chrome, Opera (V8)
Number.prototype.toExponential() 0 ๋ถ€ํ„ฐ 100 0 ๋ถ€ํ„ฐ 20
Number.prototype.toFixed() -20 ๋ถ€ํ„ฐ 100 0 ๋ถ€ํ„ฐ 20
Number.prototype.toPrecision() 1 ๋ถ€ํ„ฐ 100 1 ๋ถ€ํ„ฐ 21

์˜ˆ

์œ ํšจํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

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

์œ ํšจํ•œ ๊ฒฝ์šฐ

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 (note that it rounds up in this case)

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

์ฐธ์กฐ