Java : Currency - API Examples

Currency (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Currency methods.


Summary

Represents a currency. Currencies are identified by their ISO 4217 currency codes. Visit the ISO web site for more information.

Class diagram

System.out.println(Locale.getDefault()); // en_US

final var number = 1234;

{
    final var format = NumberFormat.getCurrencyInstance();
    System.out.println(format.format(number)); // $1,234.00

    final var currency = format.getCurrency();
    System.out.println(currency); // USD
    System.out.println(currency.getDisplayName()); // US Dollar
}
{
    final var format = NumberFormat.getCurrencyInstance(Locale.JAPAN);
    System.out.println(format.format(number)); // ¥1,234

    final var currency = format.getCurrency();
    System.out.println(currency); // JPY
    System.out.println(currency.getDisplayName()); // Japanese Yen
}

Methods

static Set<Currency> getAvailableCurrencies ()

Gets the set of available currencies.

final var currencies = Currency.getAvailableCurrencies();

currencies.stream().map(Currency::getCurrencyCode).sorted().forEach(code -> {
    System.out.println(code);
});

// Result
// ↓
//ADP
//AED
//AFA
//AFN
//ALL
//AMD
//ANG
//AOA
//...

String getCurrencyCode ()

Gets the ISO 4217 currency code of this currency.

final var currency = Currency.getInstance(Locale.US);
System.out.println(currency.getCurrencyCode()); // USD
final var currency = Currency.getInstance(Locale.JAPAN);
System.out.println(currency.getCurrencyCode()); // JPY

int getDefaultFractionDigits ()

Gets the default number of fraction digits used with this currency.

final var format = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(format.format(1234)); // $1,234.00

final var currency = format.getCurrency();
System.out.println(currency.getDefaultFractionDigits()); // 2
final var format = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format.format(1234)); // ¥1,234

final var currency = format.getCurrency();
System.out.println(currency.getDefaultFractionDigits()); // 0

String getDisplayName ()

Gets the name that is suitable for displaying this currency for the default DISPLAY locale.

System.out.println(Locale.getDefault()); // en_US

final var usd = Currency.getInstance(Locale.US);
System.out.println(usd.getDisplayName()); // US Dollar
System.out.println(usd.getDisplayName(Locale.JAPAN)); // 米ドル

final var jpy = Currency.getInstance(Locale.JAPAN);
System.out.println(jpy.getDisplayName()); // Japanese Yen
System.out.println(jpy.getDisplayName(Locale.JAPAN)); // 日本円

String getDisplayName (Locale locale)

Gets the name that is suitable for displaying this currency for the specified locale.

Please see getDisplayName().

static Currency getInstance (String currencyCode)

Returns the Currency instance for the given currency code.

System.out.println(Locale.getDefault()); // en_US

final var usd = Currency.getInstance("USD");
System.out.println(usd.getDisplayName()); // US Dollar

final var jpy = Currency.getInstance("JPY");
System.out.println(jpy.getDisplayName()); // Japanese Yen

static Currency getInstance (Locale locale)

Returns the Currency instance for the country of the given locale.

final var usd = Currency.getInstance(Locale.US);
System.out.println(usd); // USD

final var jpy = Currency.getInstance(Locale.JAPAN);
System.out.println(jpy); // JPY

int getNumericCode ()

Returns the ISO 4217 numeric code of this currency.

final var currency = Currency.getInstance(Locale.US);
System.out.println(currency.getNumericCode()); // 840
System.out.println(currency.getNumericCodeAsString()); // 840
final var currency = Currency.getInstance(Locale.JAPAN);
System.out.println(currency.getNumericCode()); // 392
System.out.println(currency.getNumericCodeAsString()); // 392
final var currency = Currency.getInstance("AUD");
System.out.println(currency.getNumericCode()); // 36
System.out.println(currency.getNumericCodeAsString()); // 036

String getNumericCodeAsString ()

Returns the 3 digit ISO 4217 numeric code of this currency as a String.

Please see getNumericCode().

String getSymbol ()

Gets the symbol of this currency for the default DISPLAY locale.

System.out.println(Locale.getDefault()); // en_US

final var usd = Currency.getInstance(Locale.US);
System.out.println(usd.getSymbol()); // $
System.out.println(usd.getSymbol(Locale.JAPAN)); // $

final var jpy = Currency.getInstance(Locale.JAPAN);
System.out.println(jpy.getSymbol()); // ¥
System.out.println(jpy.getSymbol(Locale.JAPAN)); // ¥

String getSymbol (Locale locale)

Gets the symbol of this currency for the specified locale.

Please see getSymbol().

String toString ()

Returns the ISO 4217 currency code of this currency.

final var currency = Currency.getInstance(Locale.US);
final var str = currency.toString();
System.out.println(str); // USD
final var currency = Currency.getInstance(Locale.JAPAN);
final var str = currency.toString();
System.out.println(str); // JPY

Related posts

To top of page