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
@Service public class ReportGenerationService { private final ReportRepository reportRepository;
Async report generation with Spring @Async
async
dependency-injection
completablefuture
Intermediate
7 steps
java
@Service public class ProductCatalogService { private final ProductRepository productRepository;
How Spring cache annotations keep data fresh
caching
cache-invalidation
dependency-injection
Intermediate
7 steps
java
@RestController @RequestMapping("/api/users") public class UserController {
Bean Validation in a Spring REST controller
validation
rest-api
exception-handling
Intermediate
9 steps
java
@Service public class OrderService { private final OrderRepository orderRepository;
Decoupling side effects with Spring events
event-driven
transactions
decoupling
Intermediate
8 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) { return orders.stream() .collect(Collectors.groupingBy(Order::getCustomerId)); }
Grouping streams with Java Collectors
streams
grouping
collectors
Intermediate
5 steps
java
@Service public class PaymentGatewayClient { private static final Logger log = LoggerFactory.getLogger(PaymentGatewayClient.class);
Resilient payment calls with Spring Retry
retry
backoff
fault-tolerance
Intermediate
7 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.