1

I have an instance of javax.money.CurrencyUnit and I want to display what is it's name in a given locale (for example "US Dollars", "Euro", "Japenese Yen", etc). I've read all the documentation but the only thing I was able to find is how to format a MonetaryAmount:

MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00,00,00,00.00 ยค").build()).format(myCurrencyUnit);

And I can see there I specify CurrencyStyle.NAME so it will return the currency name in the result, but the problem is I don't have a MonetaryAmount and I couldn't find a formatter for CurrencyUnit.

Thank you

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
Petar Tahchiev
  • 4,336
  • 4
  • 35
  • 48

2 Answers2

1

What I see from code is that JavaMoney doesn't support such concept as Currency Display Name. The CurrencyStyle.NAME tries to resolve Display Name from JDK Currency.

/**
 * This method tries to evaluate the localized display name for a
 * {@link CurrencyUnit}. It uses {@link Currency#getDisplayName(Locale)} if
 * the given currency code maps to a JDK {@link Currency} instance.
 * <p>
 * If not found {@code currency.getCurrencyCode()} is returned.
 *
 * @param currency The currency, not {@code null}
 * @return the formatted currency name.
 */
private String getCurrencyName(CurrencyUnit currency) {
    Currency jdkCurrency = getCurrency(currency.getCurrencyCode());
    if (Objects.nonNull(jdkCurrency)) {
        return jdkCurrency.getDisplayName(locale);
    }
    return currency.getCurrencyCode();
}

Thus you can't get display name for custom currencies like BitCoins. It was created a feature request Get display name/symbol of a CurrencyUnit

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
0

Thanks for raising this. Indeed version 1 of JavaMoney/Money JSR relies on the built in JDK resources. We discuss a new version due to demands like this one and others, please feel free to follow or comment on https://github.com/JavaMoney/jsr354-api/issues/58 in the Java Money API issue tracker.

Werner Keil
  • 592
  • 5
  • 12