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

Bug on simple rounding #3100

Closed
adshrc opened this issue Dec 7, 2023 · 7 comments
Closed

Bug on simple rounding #3100

adshrc opened this issue Dec 7, 2023 · 7 comments

Comments

@adshrc
Copy link

adshrc commented Dec 7, 2023

The issues section is used only for bug reports. Please use the Discussions section to ask questions and share ideas and suggestions.

Describe the bug
I use this library for simple rounding of currency values. Below, I want to convert 0.145 € to cents.

To Reproduce
round(0.145*100) returns 14, which should be actually 15
round(evaluate('0.145 * 100')) returns also 14, which is wrong
round(14.5) returns 15, which is correct

@josdejong
Copy link
Owner

Thanks for reporting, we should look into that! Most of the functions like equal, larger, ceil, fix do use a nearlyEqual check internally to handle round off errors like 0.145*100 = 14.499999999999998, but it looks like the function round doesn't.

@rakheesingh
Copy link

@josdejong I wanted to take this this issue.

@josdejong
Copy link
Owner

Thanks @rakheesingh! Please let me know when you need help (I haven't looked into this and I'm not sure what is needed to fix it).

@developer-diganta
Copy link

Hello @josdejong! Wanted to know if the issue is still open? Would love to give it a try!

@adshrc
Copy link
Author

adshrc commented Jan 5, 2024

adding an epsilon fixes it:

...
// to prevent this error, add an epsilon
const epsilon = 1e-12;
return round(number + epsilon, decimals);

@josdejong
Copy link
Owner

The issue is still open, thanks @developer-diganta for wanting to look into it. Help would be welcome.

@josdejong
Copy link
Owner

Fixed now in v12.4.0 via #3136.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants