Format Number with Commas in Java

Format number with Commas in Java

In this post, we will see how to format number with commas in java.

How To Add Commas to Number in Java

There are multiple ways to format number with commas in java. Let’s go through them.
[toc]

1. Using DecimalFormat

DecimalFormat can be used by providing formatting Pattern to format number with commas in java.
Here is an example:

package org.arpit.java2blog;

import java.text.DecimalFormat;

public class FormatNumberWithCommaDecimalFormat {
    public static void main(String[] args) {

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

        double d = 2000000;
        String formattedNumberWithComma = df.format(d);
        System.out.println("Formatted number with commas: "+formattedNumberWithComma);
    }
}

Output:

Formatted number with commas: 2,000,000.00

2. Using String’s format() method

You can also use String’s static method format() to format number with commas in java. This method is similar to System.out.printf.
Here is an example:

package org.arpit.java2blog;

public class FormatNumberWithCommaStringFormat {
    public static void main(String[] args) {

        double d = 2000000;
        String formattedNumberWithComma = String.format("Formatted number with commas: %,.2f", d);
        System.out.println(formattedNumberWithComma);
    }
}

Output:

Formatted number with commas: 2,000,000.00

For format String "%,.2f" means separate digit groups with commas and ".2" means round number to 2 decimal places in java.

3. Using System.out.printf

If you want to print number with commas, this is best way to print number with commas on console.
Here is an example:

package org.arpit.java2blog;

public class PrintNumberWithCommas {

    public static void main(String[] args) {

        double d = 2000000;
        System.out.printf("Formatted number with commas:  %,.0f ",d);
    }
}

Output:

Formatted number with commas: 2,000,000

4. Using Formatter

You can use java.util.Formatter‘s format() method to format number with commas in java. This is similar to System.out.printf method.
Here is an example:

package org.arpit.java2blog;

import java.util.Formatter;

public class FormatterFormatNumberWithCommas {
    public static void main(String[] args) {
        double d = 2000000;

        Formatter formatter = new Formatter();
        formatter.format("%,.2f", d);
        System.out.println("Formatted number with commas: " + formatter.toString());
    }

}

Output:

Formatted number with commas: 2,000,000.00

5. Using NumberFormat

You can also use NumberFormat‘s setMaximumFractionDigits() to put constraint on number by decimal places and use its format() method to format number with commas in java.
Here is an example:

package org.arpit.java2blog;

import java.text.NumberFormat;

public class FormatNumberWithCommansNumberFormat {

    public static void main(String[] args) {
        double d = 2000000;

        NumberFormat nf= NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);

        System.out.println("Formatted number with commas: " +nf.format(d));

    }
}

Output:

Formatted number with commas: 2,000,000

That’s all about How to format number with commas 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.