66

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..

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
anderswelt
  • 1,322
  • 1
  • 9
  • 23
  • 1
    Have you tried `.setCurrencySymbol("")`? – Dominik Sandjaja Dec 28 '11 at 16:11
  • @home: http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormatSymbols.html – Dominik Sandjaja Dec 28 '11 at 16:14
  • 4
    When there is no need for the currency, why not using `NumberFormat#getInstance( Locale )` ? – Robin Dec 28 '11 at 16:14
  • @home I was confused to.... DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols(); – jayunit100 Dec 28 '11 at 16:24
  • 1
    @home, the result is different. Example: when you use NumberFormat.getInstance(), the result could be "1,200", but when you use NumberFormat.getCurrencyInstance(), the result is "1,200.00" – Dherik Apr 10 '14 at 16:27
  • 1
    For example, Swiss Francs https://docs.microsoft.com/en-us/globalization/locale/currency-formatting _Most currencies use the same decimal and thousands separator that the numbers in the locale use, but this is not always true. In some places in Switzerland, they use the period as a decimal separator for Swiss frans (Sfr. 127.54), but then use commas as the decimal separator everywhere else (127,54)_ – Dori Aug 07 '19 at 13:40
  • You should use the response described here https://stackoverflow.com/a/54439986/9114314 – Dannie Mar 13 '20 at 14:41

12 Answers12

64

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));
JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
47

Set it with an empty string instead:

DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setCurrencySymbol(""); // Don't use null.
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(12.3456)); // 12.35
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • this is equally right as well though can only chose one answer as the right one.. – anderswelt Dec 29 '11 at 05:07
  • 2
    I think the point is what Robin wrote: There is no need for `getCurrencyInstance`, just use `getNumberInstance` – AdrianRM Oct 12 '12 at 16:27
  • I think you might also need to set `formatter.setMinimumFractionDigits(2);` in your second example. – JoshDM Dec 20 '13 at 21:46
  • @JoshDM: you shouldn't hardcode "2" here, unless you know you always want US Dollars, as the number of decimal points in currencies vary. – Joachim Sauer Jan 30 '19 at 11:52
  • @JoachimSauer I certainly can hardcode it in a 5-year old comment; I leave it up to the original requester to design a dynamic approach to setting the variable based on required Locale. – JoshDM Jan 30 '19 at 17:46
  • _I think the point is what Robin wrote: There is no need for getCurrencyInstance, just use getNumberInstance_. Not true. See my comment on the OP. – Dori Aug 07 '19 at 13:41
5

The given solution worked but ended up lefting some whitespaces for Euro for example. I ended up doing :

numberFormat.format(myNumber).replaceAll("[^0123456789.,]","");

This makes sure we have the currency formatting for a number without the currency or any other symbol.

Quentin G.
  • 888
  • 9
  • 14
  • Love me some regex. Good solution using something simple like replaceAll while still being able to account for multiple formats – Riot Goes Woof Jul 26 '19 at 17:56
  • 1
    You might want to keep the minus sign (and also parenthesis) for negative numbers. `currencyString.replaceAll("[^0123456789.,()-]","")` – Steven Spungin Sep 21 '19 at 23:21
4

Just use NumberFormat.getInstance() instead of NumberFormat.getCurrencyInstance() like follows:

val numberFormat = NumberFormat.getInstance().apply {
    this.currency = Currency.getInstance()
}

val formattedText = numberFormat.format(3.4)
Gnzlt
  • 3,142
  • 1
  • 15
  • 21
2

Maybe we can just use replace or substring to just take the number part of the formatted string.

NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.getDefault());
fmt.format(-1989.64).replace(fmt.getCurrency().getSymbol(), "");
//fmt.format(1989.64).substring(1);  //this doesn't work for negative number since its format is -$1989.64
Codingpan
  • 198
  • 6
2

I still see people answering this question in 2020, so why not

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMinimumFractionDigits(2); // <- the trick is here
System.out.println(nf.format(1000)); // <- 1,000.00
J.Adler
  • 922
  • 9
  • 16
1
DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
String formatted = df.format(num);

Works with many types for num, but don't forget to represent currency with BigDecimal.

For the situations when your num can have more than two digits after the decimal point, you could use df.setMaximumFractionDigits(2) to show only two, but that could only hide an underlying problem from whoever is running the application.

Community
  • 1
  • 1
Velizar Hristov
  • 535
  • 2
  • 9
  • 22
0

Two Line answer

NumberFormat formatCurrency = new NumberFormat.currency(symbol: "");
var currencyConverted = formatCurrency.format(money);

In TextView

new Text('${formatCurrency.format(money}'),
0

Most (all?) solutions provided here are useless in newer Java versions. Please use this:

DecimalFormat formatter = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.forLanguageTag("hr"));
formatter.setNegativeSuffix(""); // does the trick
formatter.setPositiveSuffix(""); // does the trick

formatter.format(new BigDecimal("12345.12"))
pzeszko
  • 1,509
  • 15
  • 23
0
NumberFormat numberFormat  = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.println("getCurrency = " + numberFormat.getCurrency());
        String number = numberFormat.format(99.123452323232323232323232);
        System.out.println("number = " + number);

-1

Please try below:

var totale=64000.15
var formatter = new Intl.NumberFormat('de-DE');
totaleGT=new Intl.NumberFormat('de-DE' ).format(totale)
Eray Balkanli
  • 6,960
  • 9
  • 39
  • 65
-2

there is a need for a currency format "WITHOUT the symbol", when u got huge reports or views and almost all columns represent monetary values, the symbol is annoying, there is no need for the symbol but yes for thousands separator and decimal comma. U need

new DecimalFormat("#,##0.00");

and not

new DecimalFormat("$#,##0.00");
FiruzzZ
  • 491
  • 5
  • 15
  • 3
    This is only helpful if the Locale is US, Canada, Australia, etc. It does not work for Sterling or Euro (where some countries use decimal points instead of commas for the thousand separator). – vegemite4me Nov 07 '14 at 17:54
  • 2
    The post is not about using period or commas as thousand separators, u are off topic – FiruzzZ Nov 10 '14 at 10:38
  • I still get for DecimalFormat df = new DecimalFormat("#,##0.00"); Exception in thread "main" java.text.ParseException: Unparseable number: "$400.00" – Dejell Mar 15 '15 at 17:14