How can I get the minor currency unit with rounding? #513
Replies: 2 comments
-
@DustinJSilk FYI the solution you provided is potentially erroneous as the float multiplication without rounding can create imprecise values (e.g. const minorFromUnit = halfUp(unit * Math.pow(currency.base, currency.exponent)) |
Beta Was this translation helpful? Give feedback.
-
Hey @DustinJSilk and sorry for the late reply. You're right, there currently isn't a good way to transform an object with loss and specify how the loss is handled— If you'd like to try it out, a PR is always welcome!
I would generally recommend persisting accurate, precise snapshots, and saving rounded amounts separately. Whenever you need to do further calculations, it's usually nice to have the raw, unatered data saved somewhere. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
After doing a few calculations, I've found my scale is larger than the currencies original scale.
This means that when I create a snapshot to get the amount, the amount doesn't actually represent the currencies minor value. And the toUnit method can only safely return a major unit.
For example:
In this case, I'd need to send stripe 1126, not 112560.
If I use
transformScale
, I get 1125, not 1126.This is the only way I could figure it out:
A
toMinorUnit
would be extremely helpful in this type of situation.Or potentially giving the
toUnit
function a scale option.Or even give a rounding option to transformScale. This way we can easily save snapshots in a database without worrying about saving an amount that could be different to the amount charged to customers.
Appreciate the effort!
Thanks
Beta Was this translation helpful? Give feedback.
All reactions