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

前端 未结 6 1027
醉梦人生
醉梦人生 2020-12-29 02:44

I want to get the currency format of India, so I need a Locale object for India. But there exists only few a countries that have a Locale constant

相关标签:
6条回答
  • 2020-12-29 02:57

    We have to manually create locale for India

    Locale IND = new Locale("en", "IN");
    NumberFormat india  = NumberFormat.getCurrencyInstance(IND);
    int money = 3456
    System.out.print(india.format(money));
    

    Output - Rs. 3,456

    0 讨论(0)
  • 2020-12-29 03:02
    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
    
    0 讨论(0)
  • 2020-12-29 03:14

    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);
        }
    
    
        }
    
    0 讨论(0)
  • 2020-12-29 03:15

    Look at this guide: https://docs.oracle.com/javase/1.5.0/docs/guide/intl/locale.doc.html and there is hi_IN for Hindi, India

    0 讨论(0)
  • 2020-12-29 03:18

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

    System.out.println(Currency.getInstance(new Locale("hi", "IN")).getSymbol());
    
    0 讨论(0)
  • 2020-12-29 03:18

    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 .

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