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

Rounding precision #195

Closed
ghost opened this issue Feb 23, 2022 · 4 comments
Closed

Rounding precision #195

ghost opened this issue Feb 23, 2022 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 23, 2022

Hello,

It seems I've found an issue with the rounding of numbers like 4.4249.

Currently, if I run Decimal(4.4249).toNearest('0.01', Decimal.ROUND_HALF_UP).toString(), I get 4.42.

I've tried with other functions like toSD, toDP, toPrecision, all gave me the same result.

Although, if I run Decimal(4.4249).toNearest('0.001', Decimal.ROUND_HALF_UP).toNearest('0.01', Decimal.ROUND_HALF_UP).toString(), I get 4.43.

I expected the rounding system to start from the last digit and apply rounding decimal by decimal up to the asked precision.

Is there a way to do that with decimal.js ?

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 23, 2022

x = Decimal(4.4249).toNearest('0.001', Decimal.ROUND_HALF_UP);
console.log(`x: ${x}`);    // "4.425"
y = x.toNearest('0.01', Decimal.ROUND_HALF_UP).toString();
console.log(`y: ${y}`);    // "4.43" as expected

I expected the rounding system to start from the last digit and apply rounding decimal by decimal up to the asked precision. Is there a way to do that with decimal.js ?

Sorry, I am not clear what you mean. Please refer to your example and state which digit you think should or should not be rounded up. The behaviour in the example is as expected.

@ghost
Copy link
Author

ghost commented Feb 23, 2022

x = Decimal(4.4249).toNearest('0.001', Decimal.ROUND_HALF_UP);
console.log(`x: ${x}`);    // "4.425"
y = x.toNearest('0.01', Decimal.ROUND_HALF_UP).toString();
console.log(`y: ${y}`);    // "4.43" as expected

Indeed, if I apply toNearest to each decimal position, I obtain the "4.43" as expected.

Sorry, that isn't clear. Please refer to your example and state which digit you think should or should not be rounded up. The behaviour in the example is as expected.

I expected to get "4.43" with just one step:

x = Decimal(4.4249).toNearest('0.01', Decimal.ROUND_HALF_UP);
console.log(`x: ${x}`);    // "x: 4.42", I expected "x: 4.43"

It seems decimal.js truncates the number to 3 decimals and then apply the rounding to 2 decimals.

I expected it apply the rounding for each decimal position (without doing any truncation).

For example (with more digits to be more explicit), I'm looking for a method doing all these steps automatically to have the value of e:

a = Decimal(4.4249123).toNearest('0.000001', Decimal.ROUND_HALF_UP);
console.log(`a: ${a}`);    // "a: 4.424912"
b = a.toNearest('0.00001', Decimal.ROUND_HALF_UP);
console.log(`b: ${b}`);    // "b: 4.42491"
c = b.toNearest('0.0001', Decimal.ROUND_HALF_UP);
console.log(`c: ${c}`);    // "c: 4.4249"
d = c.toNearest('0.001', Decimal.ROUND_HALF_UP);
console.log(`d: ${d}`);    // "d: 4.425"
e = d.toNearest('0.01', Decimal.ROUND_HALF_UP);
console.log(`e: ${e}`);    // "e: 4.43"

If I read the API documentation correctly, there's no such method, isn't it ?

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 23, 2022

There's no such method because rounding does not normally work that way.

If you want 4.424912 to be rounded to 4.43 you wouldn't use toNearest('0.01', Decimal.ROUND_HALF_UP) as the nearest digit in the second decimal place is clearly 2 not 3.

The following is an example of how to get 4.43

x = Decimal(4.4249).toDecimalPlaces(2, Decimal.ROUND_UP);
console.log(x);    // "4.43"

It seems decimal.js truncates the number to 3 decimals and then apply the rounding to 2 decimals.

It's not a matter of truncation, it's just that 4.4249 is nearer 4.42 then 4.43.

@ghost
Copy link
Author

ghost commented Feb 24, 2022

Thanks for your time, I better understand how decimal.js work with rounding.

@ghost ghost closed this as completed Feb 24, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant