| 669 | } |
| 670 | |
| 671 | function createRound(methodName) { |
| 672 | const func = Math[methodName] |
| 673 | return (number, precision) => { |
| 674 | precision = precision == null ? 0 : precision >= 0 ? Math.min(precision, 292) : Math.max(precision, -292) |
| 675 | if (precision) { |
| 676 | // Shift with exponential notation to avoid floating-point issues. |
| 677 | // See [MDN](https://mdn.io/round#Examples) for more details. |
| 678 | let pair = `${number}e`.split('e') |
| 679 | const value = func(`${pair[0]}e${+pair[1] + precision}`) |
| 680 | |
| 681 | pair = `${value}e`.split('e') |
| 682 | return +`${pair[0]}e${+pair[1] - precision}` |
| 683 | } |
| 684 | return func(number) |
| 685 | } |
| 686 | } |
| 687 | function round(...args) { |
| 688 | return createRound('round')(...args) |
| 689 | } |