2 * Round a number, x, to a certain number, n, of decimal places.
3 * If n < 0, keep the significative digits up to 10 ** (-n)
5 export default function withDecimalPrecision(x: number, n: number) {
6 // assume n is an integer. Round to integer otherwise
7 const powerOfTen = 10 ** Math.round(n);
8 if (powerOfTen > Number.MAX_VALUE) {
11 if (powerOfTen < Number.MIN_VALUE) {
14 return Math.round(x * powerOfTen) / powerOfTen;