need space between currency symbol and amount

前端 未结 5 799
余生分开走
余生分开走 2021-02-13 13:09

I\'m trying to print INR format currency like this:

NumberFormat fmt = NumberFormat.getCurrencyInstance();
fmt.setCurrency(Currency.getInstance(\"INR\"));
fmt.fo         


        
5条回答
  •  心在旅途
    2021-02-13 13:20

    An easier method, kind of workaround. For my locale, the currency symbol is "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);
    }
    

    Input:

    moneyFormatter(225.0);
    

    Output:

    "R$ 225,00"
    

提交回复
热议问题