Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve float decimal precision issue on round() function. #160

Merged
merged 5 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Copy link
Contributor Author

@square-li square-li Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new test case here.

${-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