Fix the reference money formatter for a refactor

Review the AI's codeEasy10 minStripe style

Your task

An AI agent replaced a hand-rolled money formatter with a new Money type across 120 call sites in your billing code, and called the change behavior-preserving. To check that claim, you run the same amounts through the old path, the new path and one reference formatter, and compare the strings.

An assistant wrote format_money(amount_minor, currency), the reference formatter. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • amount_minor: an amount in the currency's minor unit, such as cents. It can be negative.
  • currency: an ISO 4217 currency code, such as USD.
  • Return the formatted amount, or ERR.

Rules the tests check:

  • How many minor units each currency has, which is the number of digits after the decimal point:
    • 2: USD, EUR, GBP, CHF, CAD, AUD
    • 0: JPY, KRW, VND
    • 3: KWD, BHD, JOD
  • Any other code returns ERR. Codes match exactly, so usd is not USD.
  • Otherwise return the code, one space, a - if the amount is negative, then the amount without its sign in major units.
  • The major units are the whole part, a ., then exactly as many fraction digits as the currency has minor units, padded with leading zeros. With 0 minor units, write the whole amount with no ..
  • No thousands separators.

Examples

format_money(amount_minor: int, currency: str) -> str
Example 1
Inputformat_money(1234, "USD")
Output"USD 12.34"
Example 2
Inputformat_money(1234, "JPY")
Output"JPY 1234"
Example 3
Inputformat_money(1234, "KWD")
Output"KWD 1.234"