Format currency without currency symbol

JavaCurrencyNumber Formatting

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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)

Solution 5 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

Two Line answer

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

In TextView

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

Solution 10 - Java

NumberFormat numberFormat  = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.println("getCurrency = " + numberFormat.getCurrency());
        String number = numberFormat.format(99.123452323232323232323232);
        System.out.println("number = " + number);

Solution 11 - Java

here the code that with any symbol (m2, currency, kilos, etc)

fun EditText.addCurrencyFormatter(symbol: String) {

   this.addTextChangedListener(object: TextWatcher {

        private var current = ""

        override fun afterTextChanged(s: Editable?) {
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            if (s.toString() != current) {
                this@addCurrencyFormatter.removeTextChangedListener(this)

                val cleanString = s.toString().replace("\\D".toRegex(), "")
                val parsed = if (cleanString.isBlank()) 0.0 else cleanString.toInt()

                val formatter = DecimalFormat.getInstance()

                val formated = formatter.format(parsed).replace(",",".")

                current = formated
                this@addCurrencyFormatter.setText(formated + " $symbol")
                this@addCurrencyFormatter.setSelection(formated.length)

                this@addCurrencyFormatter.addTextChangedListener(this)
            }
        }
    })

}

-use with-

edit_text.addCurrencyFormatter("TL")

Solution 12 - Java

In a function like this

 fun formatWithoutCurrency(value: Any): String {
    val numberFormat = NumberFormat.getInstance()
    return numberFormat.format(value)
}

Solution 13 - Java

Please try below:

var totale=64000.15
var formatter = new Intl.NumberFormat('de-DE');
totaleGT=new Intl.NumberFormat('de-DE' ).format(totale)

Solution 14 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandersweltView Question on Stackoverflow
Solution 1 - JavaJB NizetView Answer on Stackoverflow
Solution 2 - JavaBalusCView Answer on Stackoverflow
Solution 3 - JavaQuentin G.View Answer on Stackoverflow
Solution 4 - JavaGnzltView Answer on Stackoverflow
Solution 5 - JavaVelizar HristovView Answer on Stackoverflow
Solution 6 - JavaCodingpanView Answer on Stackoverflow
Solution 7 - JavaJ.AdlerView Answer on Stackoverflow
Solution 8 - JavapzeszkoView Answer on Stackoverflow
Solution 9 - JavaVijay AnkithView Answer on Stackoverflow
Solution 10 - JavaVIJAY UPPALAView Answer on Stackoverflow
Solution 11 - JavaAbdulkerim YıldırımView Answer on Stackoverflow
Solution 12 - JavaRaheem JnrView Answer on Stackoverflow
Solution 13 - JavaVincenzo CentofantiView Answer on Stackoverflow
Solution 14 - JavaFiruzzZView Answer on Stackoverflow