java 36 lines · 6 steps

Locale-aware currency formatting in Java

A small helper that formats BigDecimal amounts as currency using a fixed locale, with correct rounding and fraction digits.

Explained by highlit
1package com.example.billing;
2 
3import java.math.BigDecimal;
4import java.math.RoundingMode;
5import java.text.NumberFormat;
6import java.util.Currency;
7import java.util.Locale;
8 
9public final class CurrencyFormatter {
10 
11 private final Locale locale;
12 
13 public CurrencyFormatter(Locale locale) {
14 this.locale = locale;
15 }
16 
17 public String format(BigDecimal amount) {
18 NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
19 Currency currency = formatter.getCurrency();
20 formatter.setRoundingMode(RoundingMode.HALF_EVEN);
21 formatter.setMinimumFractionDigits(currency.getDefaultFractionDigits());
22 formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
23 return formatter.format(amount);
24 }
25 
26 public String formatWith(BigDecimal amount, String currencyCode) {
27 Currency currency = Currency.getInstance(currencyCode);
28 NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
29 formatter.setCurrency(currency);
30 formatter.setRoundingMode(RoundingMode.HALF_EVEN);
31 int digits = currency.getDefaultFractionDigits();
32 formatter.setMinimumFractionDigits(digits);
33 formatter.setMaximumFractionDigits(digits);
34 return formatter.format(amount);
35 }
36}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Deriving fraction digits from the Currency itself keeps output correct across currencies like JPY and USD.
  2. 2HALF_EVEN rounding avoids the upward bias of HALF_UP, which matters for financial totals.
  3. 3Pairing a locale-bound formatter with an explicit currency separates how numbers look from which money they represent.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Locale-aware currency formatting in Java — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code