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 €


      • This is my code :

        package javaapplication3;
        import java.text.DecimalFormat;
        import java.text.DecimalFormatSymbols;
         * @author DONY
        public class JavaApplication3 {
             * @param args the command line arguments
            public static void main(String[] args) {
                String s = "4500";
                double harga = Double.parseDouble(s);
                DecimalFormat df = (DecimalFormat) DecimalFormat.getCurrencyInstance();
                DecimalFormatSymbols dfs = new DecimalFormatSymbols();
                String k = df.format(harga);
                System.out.println("Nilai Rupiah adalah :" + k);

        If run in command prompt with output : 4.500,00

        I want to display again to value before so from 4.500, –> 4500

        Thank you


  1. Hi Dony,

    In your code you start with a String variable called s with a value of "4500". You convert the string into double and format it for output using the DecimalFormat and return it as a k variable.

    Now back to your question, do you want to convert the k string back to number again? If yes, you can use the df.parse() method for this conversion. But if you want just to print the original value why don’t you just print the s variable.

  2. I don’t know if this is correct but what I did to print values with comma separated is

    String s = 4000;
    double d = Double.valueOf(s);
    String comma = String.format("%,.2f", d);

    and the value of comma will be 4,000.00


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.