I've had this RFC open for awhile to discuss the pros/cons of the value precision for awhile now. We should split the discussion into that thread to really hone in on the best outcome.
I appreciate the concern, as that RFC clearly shows this is a worrisome and contentious topic, but this feedback is not specific enough to be helpful. We are using this library in our point-of-sale app for manipulating different currency values and later doing transactions (both digital and physical).
Could you describe a situation where needing the extra precision is necessary for the absolute value of a currency?
As I said, the design philosophy has been that the representation of currency's value in this library matches what is actually representable of that currency.
In real terms, how do I give someone 0.001 USD? In a real transaction, this isn't possible. There is no concept for less than 0.01 USD.
It's only possible digitally, and is up to the independent service to figure out how both support and handle that case.
So, with that in mind, I'm having a hard time seeing this hypothetical invoice when trying to deal with the currency. How do you accurately conduct transactions of a fraction of a currency's unit?
item1 $1.90
item2 $2.35
--
Subtotal $4.25
--
Tax @ 9% $0.3825 // instead of $0.38
--
Total $4.6325 // instead of $0.63
when working with the rates, to calculate the $0.3825, you have access to the subtotal.amount property, which is a Foundation.Decimal, that will give you the above value of 0.3825, which can then be stored in a USD instance as USD(0.38).
@gwendal.roue Thanks for the detailed feedback.
- This is an interesting discussion to be had, outside of just
Codable support - would you be willing to file an issue regarding some hypothetical CurrencySerializer, as that sounds a lot like what you're referring to.
- As I've pointed out, there is the
amount property that is Foundation.Decimal that gives you the opportunity to work with rates and still maintain extra precision before storing it as a representation of the currency value. With regards to serializing of rates, I'm not seeing how that would fall within the scope of this library.
You mention traps with rates, would you be willing to share an example or two?
- Is your sample code an example of problems with the current design, or problems that future code would ideally solve?
@mattt Thanks for mentioning Flight-School/Money. I took inspiration from it, as well as danthorpe/Money. I made sure to credit both in the project's NOTICE.txt attribution file.