diff --git a/packages/core/src/common/number.spec.ts b/packages/core/src/common/number.spec.ts index e2546922c..c1965330e 100755 --- a/packages/core/src/common/number.spec.ts +++ b/packages/core/src/common/number.spec.ts @@ -42,39 +42,40 @@ describe.each` }); describe.each` - value | preciseValue - ${0.00345678} | ${0.003457} - ${0.02345678} | ${0.02346} - ${1.02345678} | ${1.0235} - ${10.02345678} | ${10.0235} - ${1} | ${1} - ${1.0} | ${1} - ${1.2} | ${1.2} - ${1.29} | ${1.29} - ${1.129} | ${1.129} - ${1000.1234} | ${1000.1234} - ${1000.12345} | ${1000.1235} - ${1000.02345} | ${1000.0235} - ${1000.003456} | ${1000.0035} - ${1000.000056} | ${1000.0001} - ${891289.000056} | ${891289.0001} - ${-0.00345678} | ${-0.003457} - ${-0.02345678} | ${-0.02346} - ${-1.0} | ${-1} - ${-1.2} | ${-1.2} - ${-1.29} | ${-1.29} - ${-1.129} | ${-1.129} - ${-1000.1234} | ${-1000.1234} - ${-1000.12345} | ${-1000.1235} - ${-1000.02345} | ${-1000.0235} - ${-1000.003456} | ${-1000.0035} - ${-1000.000056} | ${-1000.0001} - ${-891289.000056} | ${-891289.0001} - ${NaN} | ${NaN} - ${Infinity} | ${Infinity} - ${-Infinity} | ${-Infinity} - ${-0} | ${0} - ${0.0} | ${0} + value | preciseValue + ${0.00345678} | ${0.003457} + ${0.02345678} | ${0.02346} + ${1.02345678} | ${1.0235} + ${10.02345678} | ${10.0235} + ${1} | ${1} + ${1.0} | ${1} + ${1.2} | ${1.2} + ${1.29} | ${1.29} + ${1.129} | ${1.129} + ${1000.1234} | ${1000.1234} + ${1000.12345} | ${1000.1235} + ${1000.02345} | ${1000.0235} + ${1000.003456} | ${1000.0035} + ${1000.000056} | ${1000.0001} + ${891289.000056} | ${891289.0001} + ${28.910800000000002} | ${28.9108} + ${-0.00345678} | ${-0.003457} + ${-0.02345678} | ${-0.02346} + ${-1.0} | ${-1} + ${-1.2} | ${-1.2} + ${-1.29} | ${-1.29} + ${-1.129} | ${-1.129} + ${-1000.1234} | ${-1000.1234} + ${-1000.12345} | ${-1000.1235} + ${-1000.02345} | ${-1000.0235} + ${-1000.003456} | ${-1000.0035} + ${-1000.000056} | ${-1000.0001} + ${-891289.000056} | ${-891289.0001} + ${NaN} | ${NaN} + ${Infinity} | ${Infinity} + ${-Infinity} | ${-Infinity} + ${-0} | ${0} + ${0.0} | ${0} `('rounds number', ({ value, preciseValue }) => { test(`round(${value}) => ${preciseValue}`, () => { expect(round(value)).toBe(preciseValue); diff --git a/packages/core/src/common/number.ts b/packages/core/src/common/number.ts index 29a840f4c..2d79f0a98 100755 --- a/packages/core/src/common/number.ts +++ b/packages/core/src/common/number.ts @@ -14,13 +14,16 @@ export const round = (num: number): number => { return num; } - if (Math.abs(num) < 1) { + const absoluteValue = Math.abs(num); + if (absoluteValue < 1) { return Number(num.toPrecision(MAX_PRECISION)); } - const integer = Math.trunc(num); - const decimal = num - integer; - return integer + Number(decimal.toFixed(MAX_PRECISION)); + const integer = Math.trunc(absoluteValue); + const decimal = (absoluteValue - integer).toFixed(MAX_PRECISION).substring(2); + + const negativeSign = num === absoluteValue ? '' : '-'; + return Number(`${negativeSign}${integer}.${decimal}`); }; /**