问题 需要货币符号和金额之间的空间


我正在尝试打印INR格式的货币,如下所示:

NumberFormat fmt = NumberFormat.getCurrencyInstance();
fmt.setCurrency(Currency.getInstance("INR"));
fmt.format(30382.50);

节目 Rs30,382.50,但在印度它写的 Rs. 30,382.50(看到 http://www.flipkart.com/) 如何在没有INR硬编码的情况下解决?


2414
2017-07-12 20:01


起源



答案:


这有点像黑客,但在非常类似的情况下,我使用了这样的东西

NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
String currencySymbol = format.format(0.00).replace("0.00", "");
System.out.println(format.format(30382.50).replace(currencySymbol, currencySymbol + " "));

我必须处理的所有货币都涉及两位小数,所以我能够做到 "0.00" 对于他们所有人,但如果你打算使用像日元这样的东西,这必须进行调整。有一个 NumberFormat.getCurrency().getSymbol();但它回来了 INR 相反 Rs. 所以不能用于获取货币符号。


5
2017-07-12 20:28





看看这是否有效:

DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
fmt.setGroupingUsed(true);
fmt.setPositivePrefix("Rs. ");
fmt.setNegativePrefix("Rs. -");
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
fmt.format(30382.50);

编辑:修复了第一行。


4
2017-07-12 20:11



这看起来像你在硬编码。 - Kal
这似乎是一个很棒的解决方案!!!!! - Lalit Poptani
这是有效的,谢谢。 - Jesús Sánchez


答案:


这有点像黑客,但在非常类似的情况下,我使用了这样的东西

NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
String currencySymbol = format.format(0.00).replace("0.00", "");
System.out.println(format.format(30382.50).replace(currencySymbol, currencySymbol + " "));

我必须处理的所有货币都涉及两位小数,所以我能够做到 "0.00" 对于他们所有人,但如果你打算使用像日元这样的东西,这必须进行调整。有一个 NumberFormat.getCurrency().getSymbol();但它回来了 INR 相反 Rs. 所以不能用于获取货币符号。


5
2017-07-12 20:28





看看这是否有效:

DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
fmt.setGroupingUsed(true);
fmt.setPositivePrefix("Rs. ");
fmt.setNegativePrefix("Rs. -");
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
fmt.format(30382.50);

编辑:修复了第一行。


4
2017-07-12 20:11



这看起来像你在硬编码。 - Kal
这似乎是一个很棒的解决方案!!!!! - Lalit Poptani
这是有效的,谢谢。 - Jesús Sánchez


我没有看到任何简单的方法来做到这一点。这就是我想出来的......

获取实际货币符号的关键似乎是将目标语言环境传递给Currency.getSymbol:

currencyFormat.getCurrency().getSymbol(locale)

这里有一些代码似乎主要起作用:

public static String formatPrice(String price, Locale locale, String currencyCode) {

    NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
    Currency currency = Currency.getInstance(currencyCode);
    currencyFormat.setCurrency(currency);

    try {
        String formatted = currencyFormat.format(NumberFormat.getNumberInstance().parse(price));
        String symbol = currencyFormat.getCurrency().getSymbol(locale);

        // Different locales put the symbol on opposite sides of the amount
        // http://en.wikipedia.org/wiki/Currency_sign
        // If there is already a space (like the fr_FR locale formats things),
        // then return this as is, otherwise insert a space on either side
        // and trim the result
        if (StringUtils.contains(formatted, " " + symbol) || StringUtils.contains(formatted, symbol + " ")) {
            return formatted;
        } else {
            return StringUtils.replaceOnce(formatted, symbol, " " + symbol + " ").trim();
        }
    } catch (ParseException e) {
        // ignore
    }
    return null;
}

2
2018-01-25 00:21



不适用于负数 - 在符号和负号之间添加一个空格。由于我找不到数字和具有货币的符号之间没有空格的区域设置 后 (例如。 123.45$),我只是在符号之后添加一个空格,而不是之前。 (StringUtils.replaceOnce(formatted, symbol, symbol + " ").trim();) - hinneLinks


我不认为你可以。

你应该看看 http://site.icu-project.org/ 

icu4j可能提供更好的特定于语言环境的货币格式。


1
2017-07-12 20:28





一种更简单的方法,一种变通方法。 对于我的区域设置,货币符号为“R $”

public static String moneyFormatter(double d){

    DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
    Locale locale = Locale.getDefault();
    String symbol = Currency.getInstance(locale).getSymbol(locale);
    fmt.setGroupingUsed(true);
    fmt.setPositivePrefix(symbol + " ");
    fmt.setNegativePrefix("-" + symbol + " ");
    fmt.setMinimumFractionDigits(2);
    fmt.setMaximumFractionDigits(2);
    return fmt.format(d);
}

输入:

moneyFormatter(225.0);

输出:

"R$ 225,00"

1
2017-11-09 01:12