need space between currency symbol and amount

前端 未结 5 801
余生分开走
余生分开走 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:18

    It's a bit of a hack but in a very similar situation, I used something like this

    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 + " "));
    

    all the currencies I had to deal with involved two decimal places so i was able to do "0.00" for all of them but if you plan to use something like Japanese Yen, this has to be tweaked. There is a NumberFormat.getCurrency().getSymbol(); but it returns INR instead for Rs. so that cannot be used for getting the currency symbol.

提交回复
热议问题