Skip to content

Commit

Permalink
fix: resolve float decimal precision issue on round() function. (#160)
Browse files Browse the repository at this point in the history
* 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 <Norbert.Nader@gmail.com>
  • Loading branch information
square-li and NorbertNader committed Jul 14, 2022
1 parent 8523409 commit d95f69e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
67 changes: 34 additions & 33 deletions packages/core/src/common/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/common/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
};

/**
Expand Down

0 comments on commit d95f69e

Please sign in to comment.