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. 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));
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.
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
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.
Two Line answer
NumberFormat formatCurrency = new NumberFormat.currency(symbol: "");
var currencyConverted = formatCurrency.format(money);
In TextView
new Text('${formatCurrency.format(money}'),
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");
来源:https://stackoverflow.com/questions/8658205/format-currency-without-currency-symbol