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 set default Locale?

package org.kodejava.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class DefaultLocaleExample {
    public static void main(String[] args) {
        // Use Random class to generate some random number
        Random random = new Random();

        // We use the system default locale to format a number and a date.
        NumberFormat formatter = new DecimalFormat();
        Locale locale = Locale.getDefault();
        System.out.println("Default Locale = " + locale);
        System.out.println("Number         = " + formatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));

        // We change the default locale to Locale.ITALY by setting it through 
        // the Locale.setDefault() method, and then we format another number
        // and date using a new locale. This change will affect all the class 
        // that aware to the Locale, such as the NumberFormat class.
        Locale.setDefault(Locale.ITALY);
        NumberFormat newFormatter = new DecimalFormat();
        System.out.println("New Locale     = " + Locale.getDefault());
        System.out.println("Number         = " + newFormatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));
    }
}

The result of the code snippet above are:

Default Locale = en_US
Number         = 0.557
Date           = 9/26/21, 12:05 PM
New Locale     = it_IT
Number         = 0,217
Date           = 26/09/21, 12:05

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 parse a number for a locale?

package org.kodejava.text;

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

public class LocaleNumberParse {
    public static void main(String[] args) {
        try {
            // In this example we are trying to parse a number string in a
            // defined format. Basically we want to covert the string for a
            // locale into a correct number value.
            Number number =
                NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");

            // Just do some stuff with the number from the parse process
            if (number instanceof Long) {
                System.out.println("This number is instanceof Long and the " +
                    "value is: " + number.longValue());
            } else if (number instanceof Double) {
                System.out.println("This number is instanceof Double and the " +
                    "value is: " + number.doubleValue());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

The code snippet print out the following result:

This number is instanceof Double and the value is: 25000.75

How do I format a number for a locale?

package org.kodejava.text;

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

public class LocaleNumberFormat {
    public static void main(String[] args)  {
        // Format number for Italy locale. In Italy locale the decimal point
        // symbol is a comma.
        NumberFormat formatter = NumberFormat.getNumberInstance(Locale.ITALY);
        try {
            String number = formatter.format(195325.75);
            System.out.println("Number in Italy: " + number);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        // Format number for Japan locale. In Japan locale the decimal point
        // symbol is a dot.
        formatter = NumberFormat.getNumberInstance(Locale.JAPAN);
        try {
            String number = formatter.format(195325.75);
            System.out.println("Number in Japan: " + number);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

The code snippet output:

Number in Italy: 195.325,75
Number in Japan: 195,325.75