Skip to main content
πŸ’Ή Finance & Math Tools

What you're building

A Money class that stores an amount as integer cents (avoiding floating-point drift) and exposes arithmetic and formatting helpers.

const price = new Money(19.99);
const tax   = price.multiply(0.08);   // 8% tax
price.add(tax).format();              // "$21.59"

Requirements

Implement a Money class with:

MethodBehaviour
constructor(dollars)Store internally as rounded integer cents
add(other)Return a new Money equal to this + other
subtract(other)Return a new Money equal to this βˆ’ other
multiply(factor)Return a new Money with cents multiplied by factor, banker's rounding (round half to even)
toCents()Return the integer cent amount
format()Return a string like "$12.34" (always 2 decimal places, negative as "-$5.00")

Banker's rounding (round-half-to-even): when the fractional part is exactly 0.5, round to the nearest even integer. e.g. 2.5 β†’ 2, 3.5 β†’ 4, 4.5 β†’ 4, 5.5 β†’ 6.

Stretch goals

  • Add a Money.fromCents(cents) static factory
  • Support currency symbols other than $

What this proves

Floating-point pitfalls, integer arithmetic for money, and object-oriented design β€” the core of every payments library.

⚑ 50 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
stores cents correctly
add returns correct cents
multiply with banker's rounding
format produces dollar string

Run your code to check these.