How to format numbers in Java? - NumberFormat Example

You can use java.util.text.NumberFormat class and its method setGroupingUsed(true) and setGroupingSize(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.

If you are a beginner in Java and interested in learning essential features e.g. text formatting, I suggest to take a look at Java: A Beginner's Guide by Herbert Schildt, a must read the book for Java beginners.

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 :

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

 * Java Program to show how to add comma to numbers in Java e.g. integers.
 * Normally numbers which is used to represent monetary value e.g. 
 * price, amount etc require
 * commas to be added to improve readability and follow conventions.
 * This article shows step by
 * step example to add commas on numbers.
 * @author Javin Paul
public class NumberFormatExample{

    public static void main(String args[]) {

        // Example 1 - by using NumberFormat class
        NumberFormat myFormat = NumberFormat.getInstance();
        myFormat.setGroupingUsed(true); // this will also round numbers, 3
        // decimal places

        double[] numbers = {1.16763, 443330, 3, 517827.17};

        System.out.println("adding commas to number in Java using NumberFormat class");
        for (double d : numbers) {

        // Example 2 - By using DecimalFormat class
        DecimalFormat decimalFormat = new DecimalFormat("#.##");

        System.out.println("adding commas to number in Java using DecimalFormat class");
        for (double d : numbers) {


adding commas to number in Java using NumberFormat class
adding commas to number in Java using DecimalFormat class

Important Points to remember about adding commas to Number

1) You can use NumberFormat class or DecimalFormat class to enable grouping and introduce comma between digits.

2) You can enable grouping by calling NumberFormat.setGroupingUsed(true) method, passing true argument enable the grouping.

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.

How to add comma into number by formatting

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.

No comments:

Post a Comment