Add Up Prices in Whole Cents
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Floating-point money goes wrong (0.1 + 0.2), so keep amounts as whole cents. Export a branded type Cents and cents(n), which throws unless n is a whole number; add(...amounts), which sums them; multiply(amount, factor), which rounds to the nearest cent; and format(amount), which shows pounds and pence like £12.05 or -£2.50. Type them so a plain number is rejected wherever money is expected, while the results are Cents that can still be read as numbers.
Your code must pass
- Only takes whole cents
- Adds and multiplies in whole cents
- Formats as pounds and pence
- Has no type errors
- Rejects plain numbers as money
- Results are Cents, still read as numbers