-
Notifications
You must be signed in to change notification settings - Fork 187
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]: halfEven does not round correctly when rounding to 0 #710
Comments
…g of integer results up/down - Functions should not round integer results. up - up should only round positive numbers, 0 is not a positive number. halfUp - halfUp was incorrectly rounding numbers in the [0,1) range due to the incorrect definition of isPositive. Fixes dinerojs#710
quoted from @shasderias in #711
I agree with that statement. Committing to core division functions and throughly vetting them is a good idea. The rest could be kept from a public release until ready. Which do you think we need to focus on? |
For posterity, came across this article when I was trying to determine if there was a widely accepted way to implement rounding. It drew my attention to how odd rounding to odd was. https://www.eetimes.com/an-introduction-to-different-rounding-algorithms/ By extension: https://www.clivemaxfield.com/diycalculator/sp-round.shtml They are helpful in understanding the problem space. I think what big.js has done as the minimalist version of bignumber.js and decimal.js is instructive. http://mikemcl.github.io/big.js/ big.js implements round towards zero, round away from zero, round half up and round half even. In the context of what dinero.js is likely to be used for, they represent:
Even if the other rounding methods serve as underlying implementations, they should probably be hidden from the user until it can be demonstrated they serve some widely-used need. An opinion from someone who actually knows this stuff would probably be useful. XD |
Agreed! |
Also just run in to this issue. Wrapped every use of let taxAmount: Dinero<number>;
if (rateAmount.toJSON().amount === 0) {
taxAmount = dinero({
amount: 0,
currency: GBP,
});
} else {
taxAmount = transformScale(
rateAmount,
2,
halfEven,
);
} |
Regarding the discussion about the necessity for the rounding functions — I think they are a great value to the Dinero package. While working with financial applications it is almost inevitable that handling rounding is a requirement, so it's really convenient to have the methods to hand. |
@darrenbarklie I agree, the division operations in |
We also run into this bug (reproduction: https://stackblitz.com/edit/typescript-uee7nm?file=package.json,index.ts): const amount = dinero({
amount: 1,
scale: 2,
currency: {
code: 'EUR',
base: 10,
exponent: 2,
},
});
const rescaledAmount = transformScale(amount, 2, halfUp);
console.log(toSnapshot(rescaledAmount))
// prints { amount: 2, scale: 2 } instead of { amount: 1, scale: 2 } I can confirm that #711 is fixing this. As |
@dfr-exnaton PR #711 fixes this issue and already extends the tests for the division functions used by |
…g of integer results (#19) up/down - Functions should not round integer results. up - up should only round positive numbers, 0 is not a positive number. halfUp - halfUp was incorrectly rounding numbers in the [0,1) range due to the incorrect definition of isPositive. Fixes dinerojs#710 Co-authored-by: Lim Yue Chuan <[email protected]>
Sorry for hijacking this issue once more - as you confirmed #711 is indeed fixing these cases. Is there a chance of a |
@dfr-exnaton I'll deploy a new version tonight, thanks for your patience. |
@dfr-exnaton It's released! |
Is there an existing issue for this?
Current behavior
halfEven does not round correctly when rounding to 0
Expected behavior
Expected to see the following printed to console.
Steps to reproduce
See
https://codesandbox.io/s/nervous-raman-q7xiyt?file=/main.js
https://q7xiyt.sse.codesandbox.io/
Version
2.0.0-alpha13
Environment
Windows, Chrome - Version 108.0.5359.125 (Official Build) (64-bit)
Code of Conduct
The text was updated successfully, but these errors were encountered: