Java 货币格式化程序 : Force to format using currency symbol

标签 java currency formatter

我使用 spring 货币格式化程序根据货币代码格式化一个值

public String format(Number number, String currencyCode)
{
    CurrencyFormatter formatter = new CurrencyFormatter();
    formatter.setCurrency(Currency.getInstance(currencyCode));
    return formatter.print(number, Locale.getDefault());        
}

因此,如果我将其称为 format(10, "GBP"),那么无论区域设置如何,我都希望该值恢复为 £10.00。

这可能吗?

最佳答案

无论何时显示货币,您都需要两条信息:货币代码和显示结果的区域设置。例如,当您在 en_US 语言环境中显示 USD 时,您希望它显示 $,但在 en_AU 语言环境中,您希望它显示为 US$(因为澳大利亚的货币也称为“dollars”,他们使用 $ 符号表示澳元)。

您遇到的问题是,当您在其“非主要”区域设置中显示货币时,常用 Java 货币格式化程序很糟糕。也就是说,几乎每个美国人都希望在显示英镑和欧元时看到 £ 和 €,但在 en_US 区域设置中显示这些货币时,库存 Java 库会显示“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

关于Java 货币格式化程序 : Force to format using currency symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5466134/

相关文章:

java - 包含 * 的电话号码的正则表达式

java - 设备上的 Android 存储路径不一样?

ruby-on-rails - Ruby Money gem 的最佳舍入方法是什么?

java - 为什么我无法格式化其他两个国家的货币中国和法国

java - 格式化程序 - 显示奇怪的输出

java - 在 Java 中查找文本和点之间的文本的正则表达式

node.js - Mongoose 价格类型

c# - AutoMapper - 添加自定义格式化程序

java - Java计算器 “equal”按钮不起作用

javascript - 我应该在 Java 项目中的哪里设置前端文件?