BBackend CourseLeroy Labs
0/0
Search the course

Money & time, the two data types that break systems

Why money is NUMERIC/BigDecimal (never double), minor units for payment APIs, rounding, and why every project stores UTC while showing Africa/Accra.

● intermediateSpring-Boot-StoreHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

Money

System.out.println(0.1 + 0.2);   // 0.30000000000000004

Binary floating point can’t represent 0.1 exactly. Across millions of transactions, those errors become real money. The rules:

Layer Use
Java BigDecimal (construct from a String: new BigDecimal("0.10"), never new BigDecimal(0.1))
Postgres NUMERIC(precision, scale)
Payment APIs Integer minor units: 1999 pesewas/cents for 19.99
JSON A string ("19.99") or an integer in minor units, to avoid JS float parsing
BigDecimal total = unitPrice.multiply(BigDecimal.valueOf(qty))
                            .setScale(2, RoundingMode.HALF_EVEN);   // banker's rounding
// compare with compareTo, not equals: new BigDecimal("1.0").equals(new BigDecimal("1.00")) == false

In your projects

SperoTek (in dev)shared/domain/Money.java · 009-ledger.sql

Amounts are NUMERIC(19, 4): four decimal places for intermediate precision (FX rates, allocations), rounded at presentation. Money is a value object pairing amount with currency. The base currency is locked after the first posting, since changing it would invalidate history.

Spring-Boot-StoreStripePaymentGateway.createCheckoutSession

Stripe wants minor units, so the price is converted with setUnitAmountDecimal(unitPrice × 100), with currency "usd". Products are NUMERIC(10,2) CHECK (price > 0.00).

Hostel-Web-AppBooking.amountPaid

precision = 10, scale = 2 for amounts paid via MTN Mobile Money or bank slip.

IR3D (Kontentt)finance/application/LedgerService.java

The wallet ledger rejects any transaction where debits != credits, which is only reliable with exact decimal arithmetic.

Time

Store instants in UTC, display in local time

spring.jpa.properties.hibernate.jdbc.time_zone: UTC   # all your projects
Java type Meaning Postgres type
Instant A point on the global timeline timestamptz
OffsetDateTime An instant plus its offset timestamptz
LocalDate A calendar date with no zone (“entry date 2026-11-25”) date
LocalDateTime Wall-clock time with no zone. Ambiguous, avoid for events timestamp
ZonedDateTime An instant plus a zone with DST rules n/a; store as an instant plus a zone id

Business time vs system time

Report-Manager-2ReportManagerApplication.APP_TIME_ZONE = "Africa/Accra"

RM stores UTC but evaluates business rules (“reports due on the 5th”, “quarter closes 1 Jan/Apr/Jul/Oct”) in Africa/Accra. The schedulers use cron with a zone: @Scheduled(cron = "0 0 9 5 * *", zone = APP_TIME_ZONE). Ghana is UTC+0 with no DST, but setting the zone explicitly protects you if you ever serve other countries.

SperoTek (in dev)FiscalYear · AccountingPeriod · journal entry_date

Accounting dates are LocalDate. A journal dated 31 Dec belongs to that fiscal year no matter what time zone posted it. Created/updated timestamps are Instants.

  • Inject a Clock. Use Instant.now(clock) instead of Instant.now() so tests can freeze time.
  • DST. Some local times happen twice, and some never happen. Schedule in UTC or use zone-aware cron.
  • ISO-8601 in APIs. Use 2026-11-25T10:15:30Z, never locale formats.
  • Currency rounding rules vary by currency (JPY has 0 decimals). Use Currency.getDefaultFractionDigits().
  • Allocation without losing pennies. Splitting 100.00 three ways gives 33.33 + 33.33 + 33.34. Assign the remainder explicitly.

Check yourself

1. new BigDecimal("2.50").equals(new BigDecimal("2.5"))?
2. Type for a journal entry date in accounting?
3. Stripe amount for $19.99?