设置不带货币符号的货币格式
我正在使用 NumberFormat.getCurrencyInstance(myLocale) 来获取我给定的区域设置的自定义货币格式。但是,这始终包含我不想要的货币符号,我只想为给定的区域设置提供正确的货币数字格式,而无需货币符号。
执行 format.setCurrencySymbol(null)
会引发异常。
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
以下作品。它有点难看,但它履行了合同:
您还可以从货币格式中获取模式,删除货币符号,并从新模式重建新格式:
The following works. It's a bit ugly, but it fulfils the contract:
You could also get the pattern from the currency format, remove the currency symbol, and reconstruct a new format from the new pattern:
将其设置为空字符串:
Set it with an empty string instead:
只需使用
NumberFormat.getInstance()
而不是NumberFormat.getCurrencyInstance()
,如下所示:Just use
NumberFormat.getInstance()
instead ofNumberFormat.getCurrencyInstance()
like follows:给定的解决方案有效,但最终为欧元留下了一些空白。
我最终做了:
这确保我们拥有数字的货币格式,而无需货币或任何其他符号。
The given solution worked but ended up lefting some whitespaces for Euro for example.
I ended up doing :
This makes sure we have the currency formatting for a number without the currency or any other symbol.
我仍然看到有人在 2020 年回答这个问题,所以为什么不呢
I still see people answering this question in 2020, so why not
也许我们可以只使用替换或子字符串来获取格式化字符串的数字部分。
Maybe we can just use replace or substring to just take the number part of the formatted string.
适用于
num
的多种类型,但不要忘记 用 BigDecimal 表示货币。对于
num
小数点后可以有两位以上数字的情况,您可以使用df.setMaximumFractionDigits(2)
只显示两位,但这只能隐藏来自运行该应用程序的人的根本问题。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 usedf.setMaximumFractionDigits(2)
to show only two, but that could only hide an underlying problem from whoever is running the application.这里提供的大多数(全部?)解决方案在较新的 Java 版本中都是无用的。请使用这个:
Most (all?) solutions provided here are useless in newer Java versions. Please use this:
两行答案
TextView 中的
Two Line answer
In TextView
这里的代码与任何符号(m2、货币、公斤等)
一起使用-
here the code that with any symbol (m2, currency, kilos, etc)
-use with-
在这样的函数中
In a function like this
这对我有用。它显示它针对一种语言环境进行了硬编码,但您可以传入任何语言环境,因为两个 NumberFormat 构造函数都使用相同的语言环境(和
currency
):This does the trick for me. It shows it hardcoded for one locale, but you could pass in any locale as as both NumberFormat constructors use the same locale (and
currency
):看起来是一个更干净的解决方案,不会以一种黑客的方式改变货币符号:
出于某种原因,使用
Currency.getInstance()
在 NumberFormat 中设置货币对我来说不起作用。Looks like a cleaner solution that doesn't alter the currency symbol in a hacky way:
For some reason, setting the currency in NumberFormat with
Currency.getInstance()
didn't work for me.需要“没有符号”的货币格式,当您收到大量报告或视图并且几乎所有列都代表货币值时,该符号很烦人,不需要该符号,但是是的用于千位分隔符和小数点逗号。
你需要
和不需要
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
and not
请尝试以下:
Please try below: