Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format currency without currency symbol

I am using NumberFormat.getCurrencyInstance(myLocale) to get a custom currency format for a locale given by me. However, this always includes the currency symbol which I don't want, I just want the proper currency number format for my given locale without the currency symbol.

Doing a format.setCurrencySymbol(null) throws an exception..

like image 760
anderswelt Avatar asked Dec 28 '11 16:12

anderswelt


People also ask

How do you apply the currency style with no currency symbol?

Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want. Note: If you want to display a monetary value without a currency symbol, you can click None.

How do I change the format of cells to currency?

Select the cells that you want to format and then, in the Number group on the Home tab, click the down arrow in the Number Format box. Choose either Currency or Accounting.


1 Answers

The following works. It's a bit ugly, but it fulfils the contract:

NumberFormat nf = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(""); ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols); System.out.println(nf.format(12345.124).trim()); 

You could also get the pattern from the currency format, remove the currency symbol, and reconstruct a new format from the new pattern:

NumberFormat nf = NumberFormat.getCurrencyInstance(); String pattern = ((DecimalFormat) nf).toPattern(); String newPattern = pattern.replace("\u00A4", "").trim(); NumberFormat newFormat = new DecimalFormat(newPattern); System.out.println(newFormat.format(12345.124)); 
like image 121
JB Nizet Avatar answered Sep 28 '22 04:09

JB Nizet