Split Money Without Losing Cents

easy
Unit 6 · Decimal & Validation0/3 to milestone

Splitting 100threewaysgives100 three ways gives 33.33 + 33.33+33.33 + 33.33 = $99.99: a cent disappears. Financial code must allocate amounts exactly.

Implement with decimal.Decimal (never floats):

  • to_cents(amount): convert a string like "12.345" to integer cents using banker's rounding (ROUND_HALF_EVEN): "0.125" → 12 and "0.135" → 14
  • allocate(total_cents, weights): split integer cents in proportion to weights (ints or Decimals). Round each share down, then hand out the leftover cents one at a time to the shares with the largest remainders (ties: earlier index). The shares must sum exactly to the total.
  • format_cents(c): "$1,234.50" style, with a leading - for negatives ("-$0.05")
#decimal#money#rounding2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Split Money Without Losing Cents · TechGradify