Hacker News new | ask | show | jobs
by ThrustVectoring 2509 days ago
If you store integer amounts of the smallest currency subdivision, you're already ready for foreign currencies. You just need to specify how each currency converts from a representation like `$60` to an integer count like `6000`. This even accounts for non-decimal currencies (which basically aren't in modern use) - just set a conversion factor of 500 or 60 or whatever instead of 100.
1 comments

Probably worth noting that in some cases the currency alone isn't enough information.

For example, gas stations using USD often use more than 2 digits of precision.

I suspect (and hope) that situation is reasonably rare, though.

They can’t bill that way though and this is a monetary transfer api

Just like how the complex algorithm of determining insurance rates is likely to yield a long decimal, it has to be rounded to translate to a currency. Heck even things like sales tax being a % requires a rounding calc to be done on majority of transactions before the monetary units are tallied for the “total”

That's a price, not a balance or amount transferred. Any sub-cent stuff is handled internally by the gas station, then rounded to the nearest cent and transferred to external banking APIs. The gas station is free to use whatever representation it wants for the price and quantity, so long as it rounds to the nearest cent before pushing to the API.
Sure, I probably should have specified that I meant in general when handling money, not necessarily specifically for handling payment API calls. I've worked on a system that kept track of arbitrary precision separately while using integers to store the values. Worked reasonably well, all things considered.

But you're right, we wouldn't have expected to be able to call something like stripe with that same arbitrary precision. It was all internal pricing.