Percentage
#467
Replies: 1 comment
-
So the problem you're running into is currency calculates precision for each operation. So to break the problem down: const result = currency(18.50).divide(100) // results in 0.19, since currency rounds half up
const result2 = result.multiply(3).value // results in 0.57 The problem since the currency value itself isn't aware of the # of operations, it would be difficult to maintain the precision loss across the full scope of operations. If you need a higher precision, then yes - setting a higher precision for your operations and then converting into a currency object with your targeted precision should work: // will result in 0.56, since the operations are performed at a higher precision
const value = currency(currency(18.50, { precision: 3 }).divide(100).multiply(3), { precision: 2 }).value |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there.
I try to calculate the percentage like this:
Now I have the following variables:
The interesting part is that I would have assumed the result to be
0,56
, because when I use my calculator it is0,555
. However, since currency is rounding in between to precision of2
by default, I get0,57
as result.So there is nothing wrong with the library, just something to be aware of when calculating percentages I guess.
The solution, I think, would be either:
or maybe better say that the rounding should only happen at the end:
The former may be wrong for certain edge cases as well, because of the intermediate rounding where we need more than precision of 3.
Just wanted to leave this here for reference.
Feel free to close the issue.
Beta Was this translation helpful? Give feedback.
All reactions