Currency code to currency symbol mapping

前端 未结 6 1943
太阳男子
太阳男子 2021-02-07 09:46

Good day, in database there is table with houses for sale records. For each house record there is currency code (in ISO 4217 format) field. Is it possibly to somehow get currenc

相关标签:
6条回答
  • 2021-02-07 09:54

    There is a code-Snippet in https://stackoverflow.com/a/15343675/702345

    But be careful:

    • One Currency can have multiple symbols. E.g.: USD has also the Symbol "US$" for Local "es_US" (at least on Java8, Win7 on my VM)
    • Difference Currencies can have the same symbols, e.g.: USD, AUD
    0 讨论(0)
  • 2021-02-07 10:02

    Never blindly append currency symbols (that assumes that append is the locale-appropriate position of the currency symbol). I18n-unsafe practice. Same with the "#¤" string (for locales with prefix currency symbol, this is incorrect)

    0 讨论(0)
  • 2021-02-07 10:15

    You can use Currency class and DecimalFormat class for achieve your requirement. In following example, # represents number and ¤ represents currency symbol, you can find relevant format parameters in java API doc for DecimalFormat class.

            Currency currency = Currency.getInstance("USD");
    
            DecimalFormat decimalFormat = new DecimalFormat("#¤");
            decimalFormat.setCurrency(currency);
            System.out.println(decimalFormat.format(234));
    
    0 讨论(0)
  • 2021-02-07 10:16

    @artjomka

    I was able to reproduce your problem by setting my default locale to Latvia

    Locale.setDefault(new Locale("lv","LV"));
    Currency c  = Currency.getInstance("EUR");
    System.out.println(c.getSymbol());
    

    This gave me the output of "EUR".

    However, by leaving setting my locale to Uk (already my default) I get the symbol for the Euro(€).

    Locale.setDefault(Locale.UK);
    Currency c  = Currency.getInstance("EUR");
    System.out.println(c.getSymbol());
    
    0 讨论(0)
  • 2021-02-07 10:18

    You can use the Currency object's getSymbol method.

    What symbol is used depends on the Locale which is used See this and this.

    Update, Jan 2016: The links are now dead. But they were specific to Java 1.4/5 so not really relevant anymore. More details on currency formatting can be found in https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html. The links can be found on the WayBackEngine though.

    0 讨论(0)
  • 2021-02-07 10:18

    You should call Currency.getSymbol(Locale) rather than Currency.getSymbol() (without a locale object). Setting the default locale gives you the behavior that you want.

    0 讨论(0)
提交回复
热议问题