简体   繁体   中英

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

["

The following works.<\/i>

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

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

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.

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

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

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

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

Two Line answer

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

In TextView

new Text('${formatCurrency.format(money}'),
NumberFormat numberFormat  = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.println("getCurrency = " + numberFormat.getCurrency());
        String number = numberFormat.format(99.123452323232323232323232);
        System.out.println("number = " + number);

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

In a function like this

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

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

Please try below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM