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 @Order(Ordered.HIGHEST_PRECEDENCE) public class TenantResolutionFilter extends OncePerRequestFilter {
How a tenant-resolution filter works in Spring
multi-tenancy
servlet-filter
thread-local
Intermediate
8 steps
java
@Repository public interface SubscriptionRepository extends JpaRepository<Subscription, Long> { @Modifying(clearAutomatically = true, flushAutomatically = true)
Bulk JPQL updates in a Spring Data repository
jpql
bulk-update
modifying-query
Intermediate
5 steps
java
package com.example.uploads.config; import jakarta.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory;
Configuring upload size limits in Spring
file-upload
exception-handling
http-errors
Intermediate
6 steps
java
public final class Levenshtein { private Levenshtein() { }
Two-row Levenshtein distance in Java
dynamic-programming
edit-distance
space-optimization
Intermediate
8 steps
java
package com.example.lb; import java.util.List; import java.util.concurrent.atomic.AtomicInteger;
A thread-safe round-robin load balancer in Java
concurrency
load-balancing
round-robin
Intermediate
9 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
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.