我编写了一个 java 函数来显示每种货币的语言环境模式。请参阅下面的功能。我有兴趣知道的是,为什么当货币是瑞士法郎时,第二个小数点被硬编码为 5?请注意,我使用的是 icu 包,而 java.util.Currency 包不存在此问题。我正在使用 en_US 的默认语言环境。这是与美元和瑞士法郎货币相关的函数的输出:
Analyzing currency: [USD] localePattern: [¤#,##0.00;(¤#,##0.00)] Currency symbol [$]
Analyzing currency: [CHF] localePattern: [¤#,##0.05;(¤#,##0.05)] Currency symbol [SwF]
这是我写的java函数:
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.util.Currency;
public static void main(String[] args)
{
Currency configuredCurrency = null;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
DecimalFormat localeCurrencyFormatter = (DecimalFormat)formatter;
String localePattern = "";
String symbol = "";
String currencies = "AED,AFN,ALL,AMD,ARS,AUD,BGN,BHD,BND,BOB,BRL,BWP,BYR,CAD,CHF,CLP,CNY,COP,CRC,CZK,DJF,DKK,DOP,DZD,EEK,EGP,ERN,ETB,EUR,GBP,GTQ,HKD,HNL,HRK,HUF,IDR,ILS,INR,IQD,IRR,ISK,JOD,JPY,KES,KPW,KRW,KWD,KZT,LBP,LTL,LVL,LYD,MAD,MKD,MTL,MXN,MYR,NIO,NOK,NZD,OMR,PAB,PEN,PHP,PKR,PLN,PYG,QAR,RON,RUB,SAR,SDD,SEK,SGD,SKK,SOS,SVC,SYP,SwF,THB,TND,TRY,TZS,UAH,USD,UYU,VEB,VND,YER,ZAR,ZWD";
String[] currenciesArray = currencies.split(",");
for (int i = 0; i < currenciesArray.length; i++)
{
String currency = currenciesArray[i];
configuredCurrency = Currency.getInstance(currency);
localeCurrencyFormatter.setCurrency(configuredCurrency);
localePattern = localeCurrencyFormatter.toPattern();
symbol = localeCurrencyFormatter.getCurrency().getSymbol();
System.out.println("Analyzing currency: [" + currency + "] localePattern: [" + localePattern + "] Currency symbol [" + symbol + "]");
}
}