Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need space between currency symbol and amount

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

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

shows Rs30,382.50, but in India its written as Rs. 30,382.50(see http://www.flipkart.com/) how to solve without hardcoding for INR?

like image 809
Seeker Avatar asked Jul 12 '11 20:07

Seeker


People also ask

How do I add a space between currency symbol and number?

In English, put the currency symbol before the number with no space. For your example, put in a comma separator for the thousand.

Is there a space between number and €?

In English, the sign immediately precedes the value (for instance, €10); in most other European languages, it follows the value, usually but not always with an intervening space (for instance, 10 €, 10€). U+20A0 ₠ EURO-CURRENCY SIGN (predecessor).

Should there be a space after USD?

The dollar sign is placed to the right of the dollar figure. Use a non-breaking space after the dollar figure, and between the dollar sign and the country code: 25,99 $ US.


2 Answers

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.

like image 55
Bala R Avatar answered Oct 27 '22 00:10

Bala R


See if this works:

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

Edit: Fixed the first line.

like image 23
Jake Roussel Avatar answered Oct 26 '22 23:10

Jake Roussel