Go · Level 1 Basics

Formatting Prices

Format an amount stored in cents as a price like €12.05 in Go, with integer division, the % operator and %02d, and no float rounding errors.

  • money as integer cents
  • / and % on ints
  • %02d pads with zeros

Store money as integer cents: a float can't hold 0.10 exactly. On ints, / drops the remainder and % gives it. Trap: %d prints 5 cents as .5; %02d pads it to .05.

cents := 1205
cents / 100  // 12
cents % 100  // 5
fmt.Sprintf("%d.%02d", 12, 5)  // "12.05"

Write FormatPrice(cents int) string: the price in euros with two decimals. Example: FormatPrice(1205) → "€12.05".

solution.go
Test Suites Pending
Output