如何覆盖 Java 中的货币符号?
我正在尝试使用 Java 打印土耳其里拉(ISO 4217 货币代码 TRY)的价格。
当我这样做时,
Currency curr = Currency.getInstance("TRY");
Locale trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));
输出是:“YTL”。
然而,土耳其里拉的货币符号最近已从“YTL”更改为“TL”(如土耳其里拉的维基百科页面所示)。 使用 NumberFormat
进行格式化会得到类似的结果。
我真的不想再编写一个Currency 类,尤其是当Java 有一个内置类时。
有没有办法将 Java 的默认货币符号 TRY 覆盖为“TL”?
I'm trying to print prices in Turkish Liras (ISO 4217 currency code TRY) with Java.
When I do
Currency curr = Currency.getInstance("TRY");
Locale trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));
the output is: "YTL".
However, the currency symbol for Turkish Lira has recently changed from "YTL" to "TL" (as can be seen on the Wikipedia page for Turkish Lira). Formatting with NumberFormat
gives a similar result.
I really don't want to write yet another Currency class, especially when Java has one built-in.
Is there a way to override Java's default currency symbol for TRY to "TL"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
复活以供进一步参考:
您可以使用 DecimalFormatSymbols 更改货币以进行格式化:
或者您可以诉诸 SPI 并插入您的货币符号(也许可以通过创建您自己的国家/地区变体,例如
new Locale("tr"," TR","tl")
)。 为此,您应该为java.util.spi.CurrencyNameProvider
提供一个实现,并使用 Java 扩展机制注册它。 查看 LocaleServiceProvider 文档。Resurrection for further reference:
You can use DecimalFormatSymbols to change the Currency for formatting purposes:
Or you can appeal to SPI and plug-in your currency symbol (maybe by creating your own country variant like
new Locale("tr","TR","tl")
). In order to do that you should provide a implementation tojava.util.spi.CurrencyNameProvider
and register it with Java extension mechanism. Check out LocaleServiceProvider Documentation.有趣的是,根据维基百科,货币代码“TRL”已过时(土耳其里拉的旧代码)。 我还没有升级到最新的jdk(1.5.0_10),但是
货币 curr =Currency.getInstance("TRL");
区域设置 trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));
打印:
TL
因此,也许您可以编写一些自己的内容来将现有代码映射到过时的代码,并根据需要进行调整和测试。
Amusingly, according to Wikipedia, the currency code "TRL" is obsolete (the old code for Turkish lira). I have not upgraded to the latest jdk, (on 1.5.0_10), but
Currency curr = Currency.getInstance("TRL");
Locale trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));
Prints:
TL
So perhaps write something of your own to map existing codes to obsolete codes and adjust and test as needed.
在 Java 7 中,您可以覆盖货币属性文件。 如果您依赖 ISO-4217 符号,则可以在那里指定/覆盖它。
In Java 7 you can override currencies in a properties file. If you are relying on the ISO-4217 symbol, it can be specified/overridden there.
在您等待 Java 的同时:
For the mean time while you're waiting on Java: