Java Programming Tutorials

Java programming tutorials with many code examples!

Java Util Currency getSymbol

java.util.Currency.getSymbol()

The method java.util.Currency.getSymbol() gets the symbol of this currency for the default DISPLAY locale or ISO 4217 currency code.

It’s the same as calling getSymbol(Locale.getDefault(Locale.Category.DISPLAY)).

Method signature

The signature of the java.util.Currency.getSymbol() method is as follows:

public String getSymbol()

Parameters

The method takes no parameters.

Return value

Returns the symbol of this currency for the default DISPLAY locale or ISO 4217 currency code when the symbol cannot be determined.

Exceptions

The method throws no exceptions.

Example usage

In the following code we use java.util.Currency.getSymbol():

package com.farenda.java.util;

import java.util.Currency;
import java.util.Locale;

public class CurrencyGetSymbol {

    public static void main(String[] args) {
        System.out.println("Current default DISPLAY locale: "
                + Locale.getDefault(Locale.Category.DISPLAY));

        displaySymbol(new Locale("pl", "PL"));
        displaySymbol(Locale.US);

        Locale.setDefault(Locale.Category.DISPLAY, Locale.US);

        System.out.println("Current default DISPLAY locale: "
                + Locale.getDefault(Locale.Category.DISPLAY));

        displaySymbol(new Locale("pl", "PL"));
        displaySymbol(Locale.US);
    }

    private static void displaySymbol(Locale locale) {
        Currency currency = Currency.getInstance(locale);
        System.out.printf("Symbol of %s: %s%n",
                currency, currency.getSymbol());
    }
}

The above code produces the following output:

Current default DISPLAY locale: pl_PL
Symbol of PLN: zł
Symbol of USD: USD
Current default DISPLAY locale: en_US
Symbol of PLN: PLN
Symbol of USD: $
Share with the World!