Pages

How to format numbers in Java? - NumberFormat Example

You can use java.util.text.NumberFormat class and its method setGroupingUsed(true) andsetGroupingSize(3) to group numbers and add a comma between them. Mostly numbers which are used to represent monetary value e.g. price, amount etc requires a comma to be added to improve readability and follow conventions. For example, if your variable is storing 1 million dollars then you would like to see it as 1,000,000 rather than 1000000. Clearly the first one is more readable the second one. Of course, you can further format to add currency based upon locale, but this tutorial is not about that. In this tutorial, we are just looking to format numbers and group them. Its second part of my number formatting article, in the first part you have learned how to format floating point numbers in Java and in this article shows step by step example to group numbers and add commas between them.
There are mainly two ways to group numbers in Java, first by using NumberFormat class and second by using DecimalFormat class. Actually DecimalFormat is sub class of NumbeFormat and method used to enable grouping e.g. setGroupingUsed() is define there, but you cannot specify grouping size there.

Grouping size is the number of digits between grouping separators in the integer portion of a number and by default NumberFormat use group size of three. So if grouping is your need and you are happy with grouping size of three then go for NumberFormat but if you want advanced formatting and custom grouping size then go for DecimalFormat class.

Java Program to add comma into numbers

Here is our complete Java example to format numbers and add commas into them. All code is inside the main method to make it easy to run and understand. In order to add commas to a number, we first need to create an instance of NumberFormat class. Since NumberFormat is a singleton in Java, instead of creating we get the instance by calling NumberFormat.getInstance() method.

Now to enable grouping, we call setGroupingUsed() method and pass true, this will enable grouping, all done now. In order to print numbers with a comma, just call format() method of NumbeFormat class and it will print it accordingly.

By default, NumberFormat uses grouping size of three, so you will see comma (grouping separator) after every three digits on integer portion of number starting from the right. If you want to use a custom grouping size, you need to use DecimalFormat class. It provides a method called setGroupingSize(int size) which can customize grouping size.

Here is our Java program to add commas into numbers by using NumberFormat and DecimalFormat class :

3) Default grouping size is three, which means comma will be inserted after 3 digits from the right and only on integer portion of a number.

4) You can increase grouping size by calling a setGroupingSize(size) method of DecimalFormat class e.g. setGroupingSize(4) will add a comma after every four digits starting from a decimal point towards left.

That's all about how to add commas to a number in Java. It's just a small part of a bigger feature of formatting numbers in Java. I also suggest taking a look at a couple of beginner's Java guide to understanding more about these useful features. If you like you can follow either Head First Java 2nd Edition by Kathy Sierra or Core Java Volume 1 and 2 by Cay S. Horstmann. Both are good books and has chapters to explain text and number formatting in Java.