Java 获取没有 Locale 常量的国家/地区的货币格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2544454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:06:28  来源:igfitidea点击:

Get the currency format for a country that does not have a Locale constant

javacurrency

提问by Venkat

I want to get the currency format of India, so I need a Localeobject for India. But there exists only few a countries that have a Localeconstant (a static final Locale), and India is not one of them.

我想获取印度的货币格式,所以我需要一个Locale印度的对象。但是只有少数 a 具有Locale常数 (a static final Locale) 的国家,而印度不是其中之一。

To get the currency symbols for the US and UK, I can do the following:

要获取美国和英国的货币符号,我可以执行以下操作:

public void displayCurrencySymbols() {

    Currency currency = Currency.getInstance(Locale.US);
    System.out.println("United States: " + currency.getSymbol());

    currency = Currency.getInstance(Locale.UK);
    System.out.println("United Kingdom: " + currency.getSymbol());

}

That uses the constants Locale.USand Locale.UK. If i want to get the Indian currency format, what can I do?

使用常量Locale.USLocale.UK。如果我想获得印度货币格式,我该怎么办?

采纳答案by Thilo

According to the JDK release notes, you have locale codes hi_IN(Hindi) and en_IN(English).

根据JDK 发行说明,您有区域设置代码hi_IN(印地语)和en_IN(英语)。

System.out.println(Currency.getInstance(new Locale("hi", "IN")).getSymbol());

回答by sanjuro

Look at this guide: https://docs.oracle.com/javase/1.5.0/docs/guide/intl/locale.doc.htmland there is hi_INfor Hindi, India

看看这个指南:https: //docs.oracle.com/javase/1.5.0/docs/guide/intl/locale.doc.htmlhi_IN印地语,印度

回答by francois Bernard-Bouissieres

Here is an utility method to have the symbol, whatever is your locale

这是一个具有符号的实用方法,无论您的语言环境是什么

    public class Utils {

        public static SortedMap<Currency, Locale> currencyLocaleMap;

        static {
            currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
                @Override
                public int compare(Currency c1, Currency c2) {
                    return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
                }
            });

            for (Locale locale : Locale.getAvailableLocales()) {
                try {
                    Currency currency = Currency.getInstance(locale);
                    currencyLocaleMap.put(currency, locale);
                }
                catch (Exception e) {
                }
            }
        }


        public static String getCurrencySymbol(String currencyCode) {
            Currency currency = Currency.getInstance(currencyCode);
            return currency.getSymbol(currencyLocaleMap.get(currency));
        }

       public static String  getAmountAsFormattedString(Double amount, Double decimals, String currencyCode) {
            Currency currency = Currency.getInstance(currencyCode);
            double doubleBalance = 0.00;
            if (amount != null) {
                doubleBalance = ((Double) amount) / (Math.pow(10.0, decimals));
            }
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(currencyLocaleMap.get(currency));
            return numberFormat.format(doubleBalance);
    }


    }

回答by CleanX

heres is simple thing u can do ,

这是你可以做的简单的事情,

  float amount = 100000;

  NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));

  String moneyString = formatter.format(amount);

  System.out.println(moneyString);

The output will be , Rs.100,000.00 .

输出将为 100,000.00 卢比。

回答by a_di

import java.text.NumberFormat;
import java.util.Locale;

double dbValue = 1_23_23_213.89;
Locale lcl = new Locale("hi","IN");
NumberFormat inFrmt = NumberFormat.getNumberInstance(lcl);
System.out.println(inFrmt.format(dbValue));      //12,323,213.89

NumberFormat cur = NumberFormat.getCurrencyInstance(lcl);
System.out.println(cur.format(dbValue));      // ?12,323,213.89