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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Deriving fraction digits from the Currency itself keeps output correct across currencies like JPY and USD.
- 2HALF_EVEN rounding avoids the upward bias of HALF_UP, which matters for financial totals.
- 3Pairing a locale-bound formatter with an explicit currency separates how numbers look from which money they represent.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/locale-aware-currency-formatting-in-java-explained-java-c1e3/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.