How do I change DecimalFormat pattern?

To change the pattern use by the DecimalFormat when formatting a number we can use the DecimalFormat.applyPattern() method call. In this example we use three different patterns to format the given input number. The pattern determines what the formatted number looks like.

package org.kodejava.text;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class FormatterPattern {
    public static void main(String[] args) {
        String[] patterns = {"###,###,###.##", "000000000.00", "$###,###.##"};

        double before = 1234567.899;

        // To obtain a NumberFormat for a specific locale,
        // including the default locale, call one of NumberFormat's
        // factory methods, such as getNumberInstance(). Then cast
        // it into a DecimalFormat.
        DecimalFormat format =
                (DecimalFormat) NumberFormat.getNumberInstance(Locale.UK);
        for (String pattern : patterns) {
            // Apply the given pattern to this Format object
            format.applyPattern(pattern);

            // Gets the formatted value
            String after = format.format(before);

            System.out.printf("Input: %s, Pattern: %s, Output: %s%n",
                    before, pattern, after);
        }
    }
}

The output of the program shown below:

Input: 1234567.899, Pattern: ###,###,###.##, Output: 1,234,567.9
Input: 1234567.899, Pattern: 000000000.00, Output: 001234567.90
Input: 1234567.899, Pattern: $###,###.##, Output: $1,234,567.9

How do I format a number with leading zeros?

This example give you an example using java.text.DecimalFormat class to add leading zeros to a number. For another method you can read the see the earlier example on How do I add leading zeros to a number?.

package org.kodejava.text;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberFormatLeadingZerosExample {
    public static void main(String[] args) {
        NumberFormat formatter = new DecimalFormat("0000000");
        String number = formatter.format(2500);

        System.out.println("Number with leading zeros: " + number);
    }
}

The result of code snippet above:

Number with leading zeros: 0002500

How do I format a number?

If you want to display some numbers that is formatted to a certain pattern, either in a Java Swing application or in a JSP file, you can utilize NumberFormat and DecimalFormat class to give you the format that you want. Here is a small example that will show you how to do it.

In the code snippet below we start by creating a double variable that contains some value. By default, the toString() method of the double data type will print the money value using a scientific number format as it is greater than 10^7 (10,000,000.00). To be able to display the number without scientific number format we can use java.text.DecimalFormat which is a sub class of java.text.NumberFormat.

We create a formatter using DecimalFormat class with a pattern of #0.00. The # symbol means any number but leading zero will not be displayed. The 0 symbol will display the remaining digit and will display as zero if no digit is available.

package org.kodejava.text;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class DecimalFormatExample {
    public static void main(String[] args) {
        // We have some millions money here that we'll format its look.
        double money = 100550000.75;

        // Creates a formatter
        NumberFormat formatter = new DecimalFormat("#0.00");

        // Print the number using scientific number format and using our 
        // defined decimal format pattern above.
        System.out.println(money);
        System.out.println(formatter.format(money));
    }
}

Here is the different result of the code above.

1.0055000075E8
100550000.75