Get All Possible Available Currencies

前端 未结 8 1648
忘了有多久
忘了有多久 2021-02-05 09:17

I would like to get all possible available currencies.

Java 7 had provided such feature.

public static Set java.util.Currency.g         


        
相关标签:
8条回答
  • 2021-02-05 09:27

    I have been using Baltasarq's answer and I find it slows down my app due to a number of java.lang.IllegalArgumentException's being thrown, which is no fault of his, but actually the author of the Currency library, who has elected to throw an exception as opposed to an error (Raw Code).

    However looking into that Library, there is a public static method getAvailableCurrencies which will give you all the available currencies as a set without having to loop through all the available locales and throw a bunch of exceptions.

    The revised code now looks like this:

    public static Set<Currency> getAllCurrencies() {
        Set<Currency> currencies = Currency.getAvailableCurrencies();
        return currencies;
    }
    

    Which of course, is silly, you can just as well use the Currency's own static method and save yourself an unnecessary method call. #SaveTheStack

    0 讨论(0)
  • 2021-02-05 09:28

    Combine the ISO 4217 (xml) definition with your requirements and the sample from openJDK7 if you want all Currencies. If you iterate over the default Locales you only get 78 of 180 defined by ISO 4217.

    String[] viaLocales = { "JPY", "CNY", "SDG", "RON", "MKD", "MXN", "CAD",
        "ZAR", "AUD", "NOK", "ILS", "ISK", "SYP", "LYD", "UYU", "YER", "CSD",
        "EEK", "THB", "IDR", "LBP", "AED", "BOB", "QAR", "BHD", "HNL", "HRK",
        "COP", "ALL", "DKK", "MYR", "SEK", "RSD", "BGN", "DOP", "KRW", "LVL",
        "VEF", "CZK", "TND", "KWD", "VND", "JOD", "NZD", "PAB", "CLP", "PEN",
        "GBP", "DZD", "CHF", "RUB", "UAH", "ARS", "SAR", "EGP", "INR", "PYG",
        "TWD", "TRY", "BAM", "OMR", "SGD", "MAD", "BYR", "NIO", "HKD", "LTL",
        "SKK", "GTQ", "BRL", "EUR", "HUF", "IQD", "CRC", "PHP", "SVC", "PLN",
        "USD" };
    String[] iso4217WithoutViaLocales = { "XBB", "XBC", "XBD", "UGX", "MOP",
        "SHP", "TTD", "UYI", "KGS", "DJF", "BTN", "XBA", "HTG", "BBD", "XAU",
        "FKP", "MWK", "PGK", "XCD", "COU", "RWF", "NGN", "BSD", "XTS", "TMT",
        "GEL", "VUV", "FJD", "MVR", "AZN", "MNT", "MGA", "WST", "KMF", "GNF",
        "SBD", "BDT", "MMK", "TJS", "CVE", "MDL", "KES", "SRD", "LRD", "MUR",
        "CDF", "BMD", "USN", "CUP", "USS", "GMD", "UZS", "CUC", "ZMK", "NPR",
        "NAD", "LAK", "SZL", "XDR", "BND", "TZS", "MXV", "LSL", "KYD", "LKR",
        "ANG", "PKR", "SLL", "SCR", "GHS", "ERN", "BOV", "GIP", "IRR", "XPT",
        "BWP", "XFU", "CLF", "ETB", "STD", "XXX", "XPD", "AMD", "XPF", "JMD",
        "MRO", "BIF", "CHW", "ZWL", "AWG", "MZN", "CHE", "XOF", "KZT", "BZD",
        "XAG", "KHR", "XAF", "GYD", "AFN", "SOS", "TOP", "AOA", "KPW" };
    
    0 讨论(0)
  • 2021-02-05 09:30

    Use JodaMoney. Inside joda-money.jar I find MoneyData.csv It has all ISO 4217 currencies. Use org.joda.money.CurrencyUnit to pull currencies.

    `

    public static void main(String[] args) {
    
            List<CurrencyUnit> currencyUnits = CurrencyUnit.registeredCurrencies();
    
    
        for (CurrencyUnit currencyUnit : currencyUnits) {
            System.out.println(
                    currencyUnit.getCode() +" " + 
                    currencyUnit.getNumericCode() + " "+
                    currencyUnit.getNumeric3Code()+ " "+
                    currencyUnit.getCountryCodes());
        }
    

    `

    0 讨论(0)
  • 2021-02-05 09:31

    I am using the Currency java class but I was not able to find the Currency of Afghanistan (AFN), so I was not sure if I could rely on this Currency Library. To fix this I am using the joda-money library.

    So I put in my pom.xml this:

    <dependency> <groupId>org.joda</groupId> <artifactId>joda-money</artifactId> <version>0.11</version> </dependency>

    And in my code I use this:

    List<CurrencyUnit> currencyUnits = CurrencyUnit.registeredCurrencies();

    to get all Currencies, and the AFN was there so I am using this library now.

    I hope it can help.

    0 讨论(0)
  • 2021-02-05 09:32

    You can also use the library nv-i18n available on maven:

    <dependency>
        <groupId>com.neovisionaries</groupId>
        <artifactId>nv-i18n</artifactId>
        <version>1.22</version>
    </dependency>
    

    To see all currency codes with it, you just have to use:

    for (CurrencyCode code : CurrencyCode.values()) {
        System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName());
    }
    

    You can see more details about this class and library here: http://takahikokawasaki.github.io/nv-i18n/

    0 讨论(0)
  • 2021-02-05 09:35

    After studying the ISO table and the Currency class documentation, it seems that you can ask for currency as code or as Locale; and the class Locale has a getAvailableLocales() method.

    So, the code would be:

        public static Set<Currency> getAllCurrencies()
        {
            Set<Currency> toret = new HashSet<Currency>();
            Locale[] locs = Locale.getAvailableLocales();
    
            for(Locale loc : locs) {
                try {
                    Currency currency = Currency.getInstance( loc );
    
                    if ( currency != null ) {
                        toret.add( currency );
                    }
                } catch(Exception exc)
                {
                    // Locale not found
                }
            }
    
            return toret;
        }
    

    Hope this helps.

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