How to set thousands separator in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, setting the thousands separator is often necessary when dealing with large numbers, making them more readable by adding commas (or other locale-specific separators) to separate groups of three digits. Java provides robust support for this functionality through classes like NumberFormat and DecimalFormat from the java.text package. This article delves into the technical aspects of setting the thousands separator in Java, offering examples and explanations to help you master this skill.
Using NumberFormat for Locale-Sensitive Formatting
The NumberFormat class in Java is an abstract class that provides methods for formatting and parsing numbers in a locale-sensitive manner. It is part of the java.text package and offers a simple way to include commas as thousands separators.
Example: Using NumberFormat
Here is how you can use NumberFormat to format a number with a thousands separator:
Explanation:
- Locale: The
Locale.USspecifies that the formatting should follow the conventions used in the United States, where a comma is used as the thousands separator and a period as the decimal point.
Using DecimalFormat for Custom Formatting
DecimalFormat is a concrete subclass of NumberFormat that allows for more precise control over the formatting. It uses a pattern-based approach which provides greater flexibility.
Example: Using DecimalFormat
Below is how you can format a number using DecimalFormat:
Explanation:
- Pattern: The pattern
#,###.##is used byDecimalFormatto ensure that the integer part is separated by commas every three digits. The#represents a digit. The part after the decimal point controls the formatting of the fractional part.
Customizing Locale and Symbols
The DecimalFormat class also allows customization using DecimalFormatSymbols to adapt to different locales and specific symbol requirements.
Example: Customizing Format Symbols
Explanation:
- DecimalFormatSymbols: This allows setting custom symbols for the decimal and grouping (thousands) separators. In this example, the locale is set to
Locale.GERMANY, which uses a period as the grouping separator and a comma as the decimal separator.
Summary Table
The table below summarizes the key aspects of formatting numbers in Java:
| Feature | Class/Method | Example Code Reference | Description |
| Locale-Sensitive Formatting | NumberFormat | Number Formatter Example | Automatically adapts to locale-specific conventions. |
| Custom Pattern-Based Format | DecimalFormat | Decimal Formatter Example | Allows custom formats with pattern strings. |
| Custom Symbols | DecimalFormatSymbols | Custom Format Symbols Example | Customizes decimal and grouping separators. |
Conclusion
Understanding how to set thousands separators in Java is crucial for applications dealing with user-facing financial, scientific, or large-scale data. Whether you need locale-specific formatting or custom separators, NumberFormat and DecimalFormat provide the tools necessary to display numbers in a readable format. Leveraging these classes correctly allows you to improve the clarity and professionalism of your application's numeric outputs.
Related reading
- How to set time zone of a java.util.Date?
- How to set timeout for onFailure event (Spring, Kafka)?
- How to set timeout in Retrofit library?
- How to set up Spring Boot and log4j2 properly?
- How to set UTF-8 character encoding in Spring boot?
- How to setup multiple connection pools when multiple datasources are used in Spring Boot?
- How to setup multiple topics in a RabbitMQ Java config class using Spring Framework?
- How to setup pre-authentication header-based authentication in Spring Boot?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.