A Money Type with Currency Safety

medium
Unit 12 · Classes0/2 to milestone

Floating point is wrong for money. Implement Money(amount, currency) where amount is stored as a Decimal quantised to 2 places (ROUND_HALF_UP); accept strings, ints or Decimals (reject floats with TypeError).

Support:

  • + and - between Money of the same currency (different currencies raise ValueError)
  • * by an int or Decimal (result re-quantised)
  • comparisons <, <=, >, >=, == (same currency only for ordering; == is just False across currencies) using functools.total_ordering
  • str(m) like "12.50 GBP"
#classes#decimal#functools2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Money Type with Currency Safety · TechGradify