Why integers
Binary floating point cannot represent most decimal fractions exactly.0.1 + 0.2 is
0.30000000000000004 in every IEEE 754 language, which is every language you are
likely to be calling from.
For money that is not a rounding curiosity. Sum a few thousand order lines in floats and
your total disagrees with the sum of the same lines computed anywhere else, including in
your accounting system. Integer minor units make every amount exact and every sum
reproducible.
Converting
Multiply by the currency’s exponent when sending, divide when displaying. Do the division only at the point of display, never in the middle of a calculation.In Python use
Decimal("19.99"), not 19.99. int(19.99 * 100) is 1998, because
the float is slightly below 19.99 and int() truncates rather than rounds.Not every currency has two decimals
Assuming a factor of 100 everywhere is wrong for 23 active currencies.Zero-decimal currencies
The amount is the whole unit.5000 in JPY is ¥5,000, not ¥50.
Three-decimal currencies
The factor is 1000.1999 in KWD is 1.999 KWD, not 19.99.
Everything else
Two decimals, factor 100. USD, EUR, GBP, INR and the rest.Currency codes
Send the ISO 4217 alphabetic code. Case is not significant on input, sousd and USD
are both accepted.
A code that is not an active ISO 4217 currency is refused:
?currency=yen is refused rather than treated as
an unknown code and relabelled onto a price that was never converted, which would show a
shopper a dollar amount with a yen label on it.
