How do I change number format symbols?

You can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers. These symbols include the decimal separator which can be changed using the setDecimalSeparator(), the grouping separator which can be change using the setGroupingSeparator() method. You can also alter the minus sign, and the percent sign, among others.

package org.kodejava.text;

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

public class NumberFormatSymbol {
    public static void main(String[] args) {
        DecimalFormat formatter;
        String pattern = "###,###.##";
        double number = 123456.789;

        // Create a DecimalFormatSymbols object for the United States
        // locale.
        DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);

        // Create a format object with the given pattern without
        // change the locale dfs then format the given value.
        formatter = new DecimalFormat(pattern);
        String before = formatter.format(number);

        // Change the decimal separator and grouping separator symbol.
        dfs.setDecimalSeparator(',');
        dfs.setGroupingSeparator('.');
        dfs.setMinusSign('-');
        dfs.setPercent('%');

        // Create a format object with the given pattern and symbol
        // then format the given value.
        formatter = new DecimalFormat(pattern, dfs);
        String after = formatter.format(number);

        System.out.println("before: " + before + " | after: " + after);
    }
}

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 number as percentage string?

The NumberFormat.getPercentInstance() method returns a percentage format for the specified locale. In this example we will format the number as percentage using the Locale.US locale.

package org.kodejava.text;

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

public class LocalePercentageFormat {
    public static void main(String[] args) {
        // Format percentage for Locale.US locale with this formatter,
        // a decimal fraction such as 0.75 is displayed as 75%
        double number = 0.25;
        NumberFormat format = NumberFormat.getPercentInstance(Locale.US);
        String percentage = format.format(number);
        System.out.println(number + " in percentage = " + percentage);
    }
}

The following line is the output of the program:

0.25 in percentage = 25%

How do I format a number as currency string?

Creating a financial application requires you to format number as a currency. It should include the correct thousand separator, decimal separator and the currency symbol. For this purpose you can use the NumberFormat.getCurrencyInstance() method and pass the correct Locale to get the currency format that you want.

package org.kodejava.text;

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

public class LocaleCurrencyFormat {
    public static void main(String[] args) {
        Double number = 1500D;

        // Format currency for Canada locale in Canada locale, 
        // the decimal point symbol is a comma and currency
        // symbol is $.
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
        String currency = format.format(number);
        System.out.println("Currency in Canada : " + currency);

        // Format currency for Germany locale in German locale,
        // the decimal point symbol is a dot and currency symbol
        // is €.
        format = NumberFormat.getCurrencyInstance(Locale.GERMANY);
        currency = format.format(number);
        System.out.println("Currency in Germany: " + currency);
    }
}

Here is an output for the currency format using the Locale.CANADA and Locale.GERMANY.

Currency in Canada : $1,500.00
Currency in Germany: 1.500,00 €

How do I create a Locale object using a variant?

Font differences may force you to use different characters on different platforms. You could then define the Locale objects with the variant codes to identify those differences. The variant codes conform to no standard. They are arbitrary and specific to your application. If you create Locale objects with variant codes only your application will know how to deal with them.

In this example instead of demonstrating to use a different font for different platform we simplify it to just print a different message. We create three different resource bundles for each platform, the Birthday_fr_FR_UNIX.properties, Birthday_fr_FR_MAC.properties and Birthday_fr_FR_WIN.properties. These files will contains different message for each platform.

package org.kodejava.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class LocalePlatform {
    public static void main(String[] args) {
        ResourceBundle res;
        String language = "fr";
        String country = "FR";

        // Construct a locale from language, country, variant. Where the variant
        // can be a variant vendor and browser specific code.
        Locale unix = new Locale(language, country, "UNIX");
        res = ResourceBundle.getBundle("Birthday", unix);
        System.out.println("UNIX: " + res.getString("birthday"));

        Locale mac = new Locale(language, country, "MAC");
        res = ResourceBundle.getBundle("Birthday", mac);
        System.out.println("MAC : " + res.getString("birthday"));

        Locale windows = new Locale(language, country, "WIN");
        res = ResourceBundle.getBundle("Birthday", windows);
        System.out.println("WIN : " + res.getString("birthday"));
    }
}

Here are the contents of the resource bundle files:

Birthday_fr_FR_UNIX.properties

birthday=Unix, Joyeux anniversaire à vous!

Birthday_fr_FR_MAC.properties

birthday=Mac, Joyeux anniversaire à vous!

Birthday_fr_FR_WIN.properties

birthday=Windows, Joyeux anniversaire à vous!