From d95f69ee8c06143e338f373d7fd23b3f4f669415 Mon Sep 17 00:00:00 2001 From: "Xiao(Bill) Li" Date: Thu, 14 Jul 2022 13:14:51 -0400 Subject: [PATCH] fix: resolve float decimal precision issue on round() function. (#160) * fix: update @testing-library/react to use create root * fix: resolve float decimal precision issue on round() function. * Simplify round() function. Remove managing the negative sign separately. Co-authored-by: Norbert Nader --- packages/core/src/common/number.spec.ts | 67 +++++++++++++------------ packages/core/src/common/number.ts | 9 ++-- 2 files changed, 40 insertions(+), 36 deletions(-) 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..8a89d924b 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)); + // in case of negative number, we need to remove the first 3 characters from decimal string eg. -0.123 => 123 + const decimal = (num - integer).toFixed(MAX_PRECISION).substring(num !== absoluteValue ? 3 : 2); + + return Number(`${integer}.${decimal}`); }; /**