Java DecimalFormat example

In this post, we will see how to format a number using DecimalFormat in java.

DecimalFormat

DecimalFormat class is subclass of NumberFormat class and it is used to format numbers by using specify formatting pattern.We can format a number upto 2 decimal place,upto 3 decimal place, using comma to separate digits.

Creating DecimalFormat object

String pattern="###,###.00";
DecimalFormat df=new DecimalFormat(pattern);

The string(i.e  pattern) we pass in DecimalFormat constructor specify format for the number.

DecimalFormat’s format method can be used to format a number.Once we create object of DecimalFormat object, we can call format method and pass the number as parameter.

String number=df.format(123456789.123);

Few  Number Format pattern are:

[table id=9 /]

Below examples will help you understand better.

[table id=8 /]
Below example shows the use of DecimalFormat in java:- 

package org.arpit.java2blog;

import java.text.DecimalFormat;  //import  package
public class DecimalFormatExample
{
    public static void main(String args[])
    {
        //formatting number upto 2 decimal places
        String pattern1="###,###.00";    // pattern according to which number will be formatted
        DecimalFormat df=new DecimalFormat(pattern1);   //object creation and initialized with string
        System.out.println(df.format(987654341.9));   //pass number to format method for formatting
        System.out.println(df.format(7654341.987));

        //formatting upto 3 decimal places
        String pattern2="#,###.000";
        df=new DecimalFormat(pattern2);
        System.out.println(df.format(100000098));
        System.out.println(df.format(10000000.98));
        System.out.println(df.format(10000000.98789));
    }
}
When you run above program, you will get below output

Output

987,675,341.90
7,654,341.99
1,000,000,980.000
10,000,000.980
10,000,000.987

In our above example, when we format upto 2 decimal place then number having more than two digits after decimal will print only two digits and in upto 3 decimal place will print upto only 3 digits if less than three digits are there than it will placed zero in the end.

Grouping the digits

You can group the digits starting from decimal by using method setGroupSize() on DecimalFormat object.

package org.arpit.java2blog;

import java.text.DecimalFormat;  //import  package
public class DecimalFormatExample
{
    public static void main(String args[])
    {
        //formatting number upto 2 decimal places
        String pattern1="###,###.00";    // pattern according to which number will be format

        DecimalFormat df=new DecimalFormat(pattern1);   //object creation and initialized with string
        df.setGroupingSize(4);
        System.out.println(df.format(987654341.9));   //pass number to format method for formatting
    }
}

When you run above program, you will get below output

9,8765,4341.90

Please note that DecimalFormat is not thread safe means in multithreading environment don’t use DecimaFormat.

That’s all about DecimalFormat in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.