Fix the minor unit formatter for a payments API
Your task
Your payments API sends amounts as 64-bit integers in minor units (cents for USD), written as strings. An AI's serializer did Number(t.amountMinor) / 100, and after a release the mobile app showed wrong amounts for large transactions. The new formatter builds the display string without dividing.
An assistant wrote format_minor_units(amounts_minor, exponent). It is in the editor. It has a bug. Find it and fix it.
Input and output:
amounts_minor: amounts in minor units, as strings, such as"1999".exponent: how many minor digits the currency has: 2 for USD, 0 for JPY, 3 for KWD.- Return one display string per amount, in order, or
[]whenexponentis below 0.
Rules the tests check:
- A valid amount is an optional
-followed by one or more digits from 0 to 9. Anything else gives"invalid"for that entry. - The result must be exact for every digit, however long the amount is.
- Drop leading zeros. Put a decimal point
exponentdigits from the right, and pad with zeros on the left so at least one digit comes before the point. With an exponent of 0 there is no point. - A negative amount gets a
-in front, unless it is zero:"-0"gives"0.00".
Examples
format_minor_units(amounts_minor: list[str], exponent: int) -> list[str]Example 1
Input
format_minor_units(["1999", "100", "9007199254740993"], 2)Output
["19.99", "1.00", "90071992547409.93"]Example 2
Input
format_minor_units(["5", "-250", "0"], 2)Output
["0.05", "-2.50", "0.00"]Example 3
Input
format_minor_units(["1500", ""], 0)Output
["1500", "invalid"]