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

See*_*ker 12 java currency-formatting

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

NumberFormat fmt = NumberFormat.getCurrencyInstance();
fmt.setCurrency(Currency.getInstance("INR"));
fmt.format(30382.50);
Run Code Online (Sandbox Code Playgroud)

显示Rs30,382.50,但在印度,其编写为Rs. 30,382.50(请参阅http://www.flipkart.com/)如何在没有硬编码的情况下解决INR?

Bal*_*a R 6

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

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 + " "));
Run Code Online (Sandbox Code Playgroud)

我必须处理的所有货币都包含两个小数位,所以我能够为所有这些货币做"0.00",但如果你打算使用像日元这样的东西,这必须进行调整.有一个NumberFormat.getCurrency().getSymbol(); 但它返回INR,Rs.因此不能用于获取货币符号.


小智 6

一种更简单的方法,一种解决方法。对于我的区域设置,货币符号是“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);
}
Run Code Online (Sandbox Code Playgroud)

输入:

moneyFormatter(225.0);
Run Code Online (Sandbox Code Playgroud)

输出:

"R$ 225,00"
Run Code Online (Sandbox Code Playgroud)


Jak*_*sel 5

查看是否可行:

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

编辑:修复了第一行。