Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串构造货币区域设置?_Java_Locale_Currency_Number Formatting - Fatal编程技术网

Java 如何从字符串构造货币区域设置?

Java 如何从字符串构造货币区域设置?,java,locale,currency,number-formatting,Java,Locale,Currency,Number Formatting,如何从同时包含字符和数字的字符串中获取区域设置?有可能吗?我在java方面还不是非常先进,我担心这会进入到这个比喻池的更深处 说我有 String userInput = "250.00 SEK"; 从这一点上,我想装置打印“瑞典”的方法。 所以我只知道货币代码,我想要该地区的显示名。我想你唯一的希望是手工构建一个地图,并根据货币字符串末尾的国家代码进行查找。从一种货币到一个区域设置(至少我知道)没有自动的方法。您可以将该类与中使用的代码结合使用,假设所有货币都有3个字符(),您可以

如何从同时包含字符和数字的字符串中获取区域设置?有可能吗?我在java方面还不是非常先进,我担心这会进入到这个比喻池的更深处

说我有

String userInput = "250.00 SEK";
从这一点上,我想装置打印“瑞典”的方法。
所以我只知道货币代码,我想要该地区的显示名。

我想你唯一的希望是手工构建一个
地图,并根据货币字符串末尾的国家代码进行查找。从一种货币到一个区域设置(至少我知道)没有自动的方法。

您可以将该类与中使用的代码结合使用,假设所有货币都有3个字符(),您可以

    String code = userInput.substring(userInput.length()-3,userInput.length());
    state = Currency.valueOf(code);
    system.out.print(state.name);


public enum Currency{
  SEK("Sweden"),
  GBP("British Pound");

  private String name;

  public Currency(String n) { this.name = n; }
  public String toString() { return this.name; }
}
String userInput=“250.00瑞典克朗”;
最终设置AvailableUrrences=Currency.getAvailableUrrences();
for(货币可用性:可用性){
最终字符串currencyCode=availableCurrency.getCurrencyCode();
最终字符串displayName=availableCurrency.getDisplayName();
if(userInput.contains(currencyCode)){
System.out.println(“货币为”+availableCurrency.getDisplayName());
}
}

货币是瑞典克朗

假设您的字符串中始终有空间,您可以将其按空间分割,然后按返回数组中的第二个值获取货币实例

String userInput = "250.00 SEK";
String[] parts = userInput.split(" ");

Currency currency = Currency.getInstance(parts[1]);

System.out.print(currency.getDisplayName());
String userInput=“250.00瑞典克朗”;
最终设置AvailableUrrences=Currency.getAvailableUrrences();
for(货币可用性:可用性){
最后一个字符串strCode=availableCurrency.getCurrencyCode();
最终字符串currencyCode=strCode;
if(userInput.contains(currencyCode)){
对于(区域设置:NumberFormat.getAvailableLocales()){
字符串代码=NumberFormat.getCurrencyInstance(locale.getCurrency().getCurrencyCode();
if(strCode.equals(code)){
System.out.println(locale.getDisplayCountry());;
}
}
}
}

将为您提供:Sweden

如果格式相对相同,您可以使用regexp拆分字符串
String userInput = "250.00 SEK";
String[] parts = userInput.split(" ");

Currency currency = Currency.getInstance(parts[1]);

System.out.print(currency.getDisplayName());
String userInput = "250.00 SEK";
    final Set<Currency> availableCurrencies = Currency.getAvailableCurrencies();
    for (Currency availableCurrency : availableCurrencies) {
        final String strCode = availableCurrency.getCurrencyCode();
        final String currencyCode = strCode;
        if (userInput.contains(currencyCode)){
            for (Locale locale : NumberFormat.getAvailableLocales()) {
                String code = NumberFormat.getCurrencyInstance(locale).getCurrency().getCurrencyCode();
                if (strCode.equals(code)) {
                    System.out.println(locale.getDisplayCountry());;
                }
            }
        }
    }