통화를 표시 할 때마다 통화 코드와 결과를 표시하는 로캘의 두 가지 정보가 필요합니다. 예를 들어, en_US 로켈에 USD를 표시하면 $를 표시하지만 en_AU 로켈에서는 US $로 표시하려고합니다 (오스트레일리아 통화는 "달러"라고도하고 $ 기호는 AUD).
"기본이 아닌"로켈에서 통화를 표시 할 때 주식 Java 통화 포맷터가 악취를 받는다는 문제가 있습니다. 즉, 파운드와 유로를 표시 할 때 미국의 거의 모든 사람들이 파운드와 유로를 표시하려고하지만 주식 Java 라이브러리는 en_US 로켈에서 해당 통화를 표시 할 때 "GBP"와 "EUR"를 표시합니다.
다음 코드로이 문제를 해결했습니다. 나는 기본적으로 국제 통화 표시 할 때 사용하는 내 자신의 심볼을 지정
public static NumberFormat newCurrencyFormat(Currency currency, Locale displayLocale) {
NumberFormat retVal = NumberFormat.getCurrencyInstance(displayLocale);
retVal.setCurrency(currency);
//The default JDK handles situations well when the currency is the default currency for the locale
if (currency.equals(Currency.getInstance(displayLocale))) {
return retVal;
}
//otherwise we need to "fix things up" when displaying a non-native currency
if (retVal instanceof DecimalFormat) {
DecimalFormat decimalFormat = (DecimalFormat) retVal;
String correctedI18NSymbol = getCorrectedInternationalCurrencySymbol(currency, displayLocale);
if (correctedI18NSymbol != null) {
DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols(); //this returns a clone of DFS
dfs.setInternationalCurrencySymbol(correctedI18NSymbol);
dfs.setCurrencySymbol(correctedI18NSymbol);
decimalFormat.setDecimalFormatSymbols(dfs);
}
}
return retVal;
}
private static String getCorrectedInternationalCurrencySymbol(Currency currency, Locale displayLocale) {
ResourceBundle i18nSymbolsResourceBundle =
ResourceBundle.getBundle("correctedI18nCurrencySymbols", displayLocale);
if (i18nSymbolsResourceBundle.containsKey(currency.getCurrencyCode())) {
return i18nSymbolsResourceBundle.getString(currency.getCurrencyCode());
} else {
return currency.getCurrencyCode();
}
}
을 그리고 내가 사용하는 통화 기호를 지정할 경우 내 속성 (correctedI18nCurrencySymbols.properties를) 파일이 있습니다
# Note that these are the currency symbols to use for the specified code when displaying in a DIFFERENT locale than
# the home locale of the currency. This file can be edited as needed. In addition, if in some case one specific locale
# would use a symbol DIFFERENT than the standard international one listed here, then an additional properties file
# can be added making use of the standard ResourceBundle loading algorithm. For example, if we decided we wanted to
# show US dollars as just $ instead of US$ when in the UK, we could create a file i18nCurrencySymbols_en_GB.properties
# with the entry USD=$
ARS=$AR
AUD=AU$
BOB=$b
BRL=R$
CAD=CAN$
CLP=Ch$
COP=COL$
CRC=\u20A1
HRK=kn
CZK=K\u010D
DOP=RD$
XCD=EC$
EUR=\u20AC
GTQ=Q
GYD=G$
HNL=L
HKD=HK$
HUF=Ft
INR=\u20B9
IDR=Rp
ILS=\u20AA
JMD=J$
JPY=JP\u00A5
KRW=\u20A9
NZD=NZ$
NIO=C$
PAB=B/.
PYG=Gs
PEN=S/.
PHP=\u20B1
PLN=\u007A\u0142
RON=lei
SGD=S$
ZAR=R
TWD=NT$
THB=\u0E3F
TTD=TT$
GBP=\u00A3
USD=US$
UYU=$U
VEF=Bs
VND=\u20AB
아니, 내 질문했다 로케일을 사용하여 포맷하고 싶지 않습니다. 나는 파운드 스털링에 대한 상징을 말해달라고 말하고 나에게 Locale을 말하지 않고 나를 돌려 주어야한다. 영국. – Prasanna
Java는 로케일없이 포맷하는 방법을 어떻게 알 수 있습니까? 통화 기호, 소수점 구분 기호 및 기타 서식 설정과 같은 설정을 지정하는 수단입니다. 그렇지 않으면 원하는 작업을 수행하기 위해 수동 문자열 조작을 사용해야합니다. –