Fix the money formatter for global orders
Your task
An AI wrote a TypeScript helper that formats order totals. It printed a dollar sign and two decimals for every order: fine on the US test account, wrong for everyone else. Your team is replacing it with a formatter driven by the currency and the locale. In production that is Intl.NumberFormat. This version writes the ISO currency code in place of a symbol, so the output stays plain ASCII.
An assistant wrote format_money(amount_minor, currency, locale). It is in the editor. It has a bug. Find it and fix it.
Input and output:
amount_minor: the amount in the currency's smallest unit, like cents. It can be negative.currency: an ISO 4217 currency code, like"USD", in any case. It can be empty.locale: a locale tag, like"en-US".- Return the formatted amount as a string.
Rules the tests check:
- Uppercase the currency code.
- The currency's minor units are the digits after the decimal separator. They are 0 for
JPY,KRW,VND,CLPandISK, and 3 forKWD,BHD,JOD,OMRandTND. Every other code has 2, including an empty or unknown one. - The locale, matched exactly, sets the separators and where the code goes:
de-DE,es-ESandit-IT: group with., decimal,, code after the number.fr-FR: group with a space, decimal,, code after the number.- Any other locale, including
"": group with,, decimal., code before the number.
- Split the absolute amount into a whole part and its last minor-unit digits. Pad with leading zeros so the whole part has at least one digit: 5 cents is
0.05. With 0 minor units there is no decimal separator. - Group the whole part in threes from the right.
- Join the code and the number with one space. With an empty code, return the number alone.
- A negative amount gets one
-in front of the whole result, code included.
Examples
format_money(amount_minor: int, currency: str, locale: str) -> strExample 1
Input
format_money(129900, "USD", "en-US")Output
"USD 1,299.00"Example 2
Input
format_money(129900, "EUR", "de-DE")Output
"1.299,00 EUR"Example 3
Input
format_money(129900, "JPY", "ja-JP")Output
"JPY 129,900"