格式化货币没有货币符号 [英] Format currency without currency symbol

查看:180
本文介绍了格式化货币没有货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NumberFormat.getCurrencyInstance(myLocale)来获取我提供的区域设置的自定义货币格式。但是,这总是包含我不想要的货币符号,我只想要我没有货币符号的给定区域设置的正确货币编号格式。

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.

执行 format.setCurrencySymbol(null)抛出异常..

推荐答案

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

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

这篇关于格式化货币没有货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆