Day 52: Number Formatting: Bridging the gap between intl and BigDecimal for localized UIs
In Day 51, we established a rigorous foundation for temporal data by decoupling "wall time" from UTC, ensuring that our travel booking intentions remain accurate regardless of the user's local timezone. Today, we address the presentation layer of that dataβspecifically, how we translate raw numeric state into human-readable currency and distance strings that don't look like an amateur built them.
The Problem: Floating Point is a Lie
In distributed systems, we represent money as integers (cents) or arbitrary-precision decimals to avoid the catastrophic rounding errors inherent in IEEE 754 floating-point math. When you fetch a price from a backend, you receive a long or a string representing the exact amount.
The trap is assuming that formatting this for the UI is a simple string concatenation. If you simply display 1250.5 for a price, you fail users in regions that use commas as decimal separators (e.g., 1.250,50 in Germany). Worse, you ignore currency symbols, their placement, and the specific grouping separators that vary by locale.
Keep going with the full lesson
The rest of this lesson β plus its implementation guide, runnable scripts and source code β is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in