Explain Codes LogoExplain Codes Logo

How to set thousands separator in Java?

java
decimalformat
bigdecimal
numberformat
Nikita BarsukovbyNikita Barsukov·Aug 13, 2024
TLDR

Ready to introduce a thousands separator in Java? Utilize NumberFormat:

NumberFormat formatter = NumberFormat.getInstance(); System.out.println(formatter.format(1000000)); // Prints "1,000,000", boom!

This code initializes NumberFormat for the default locale, and with the format() function, we've got the thousands separator!

Advanced techniques using DecimalFormat

NumberFormat is great, but when you crave more control, meet DecimalFormat. It's a secret agent with a license to format:

DecimalFormat formatter = new DecimalFormat("#,###"); System.out.println(formatter.format(123456789)); // Prints "123,456,789". Tada!

Let's add a twist – if you're bored with commas, customize the grouping separators:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); symbols.setGroupingSeparator(' '); // Move aside, commas! Space is the new chic. DecimalFormat customFormatter = new DecimalFormat("#,###", symbols); System.out.println(customFormatter.format(123456789)); // Prints "123 456 789". Fabulous!

Want to bring in a little more culture? Try locale-specific formatting, as in Brazilian Portuguese:

DecimalFormatSymbols brSymbols = new DecimalFormatSymbols(new Locale("pt", "BR")); DecimalFormat brFormatter = new DecimalFormat("#,##0.00", brSymbols); System.out.println(brFormatter.format(new BigDecimal("12345.67"))); // Prints "12.345,67". Oi Brasil!

Here's the thing, when dealing with BigDecimal's longValue() method, we should watch out for precision loss. And remember, BigDecimal is your best mate with large numbers and precision.

Simple formatting using strings

For quick and dirty formatting, String.format is your fast and furious friend:

System.out.println(String.format("%,d", 1000000)); // Prints "1,000,000" for integers. Fast... System.out.println(String.format("%,.2f", 1234567.89)); // Prints "1,234,567.89" for floats. ...And furious!

Scaling the performance game

Size matters! When dealing with large-scale applications, remember - every formatting operation counts. An efficient implementation using NumberFormat or DecimalFormat is a must in the reusable components department.

Dealing with various formatting scenarios

Embracing geographical variety

Different locales, different rules. Switch the rules to the game with Locale object and NumberFormat:

System.out.println(NumberFormat.getNumberInstance(Locale.FRANCE).format(1000000)); // Prints "1 000 000" - Bonjour! System.out.println(NumberFormat.getNumberInstance(Locale.GERMANY).format(1000000)); // Prints "1.000.000" - Guten tag!

Surfing the float wave

For floats, rise up to the occasion with custom patterns and DecimalFormat:

DecimalFormat floatFormatter = new DecimalFormat("#,##0.00"); System.out.println(floatFormatter.format(1234567.89)); // Prints "1,234,567.89"

Landing back, beware of float's rounding behavior and precision loss.

Best practices

Here are key points to remember:

  1. BigDecimal for absolute precision.
  2. DecimalFormat and DecimalFormatSymbols for tailor-made patterns and symbols.
  3. Efficiency is the key, particularly in high-load scenarios.