Java : Currency with Examples
Currency (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Currency methods.
Summary
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 ()
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 ()
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 ()
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 ()
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)
Please see getDisplayName().
static Currency getInstance (String currencyCode)
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)
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 ()
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 ()
Please see getNumericCode().
String getSymbol ()
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)
Please see getSymbol().
String toString ()
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
- API Examples