且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

格式化货币没有货币符号

更新时间:2023-10-03 23:42:04

以下作品。这有点难看,但它符合合同:

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