如何覆盖 Java 中的货币符号?

发布于 2024-07-20 05:11:55 字数 442 浏览 8 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北风几吹夏 2024-07-27 05:11:56

复活以供进一步参考:

您可以使用 DecimalFormatSymbols 更改货币以进行格式化:

Locale trLocale = new Locale("tr", "TR");
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(trLocale);
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(trLocale);
dfs.setCurrencySymbol("TL");
decimalFormat.setDecimalFormatSymbols(dfs);

或者您可以诉诸 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:

Locale trLocale = new Locale("tr", "TR");
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(trLocale);
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(trLocale);
dfs.setCurrencySymbol("TL");
decimalFormat.setDecimalFormatSymbols(dfs);

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 to java.util.spi.CurrencyNameProvider and register it with Java extension mechanism. Check out LocaleServiceProvider Documentation.

秋意浓 2024-07-27 05:11:56

有趣的是,根据维基百科,货币代码“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.

萌逼全场 2024-07-27 05:11:56

在 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.

无声静候 2024-07-27 05:11:56

在您等待 Java 的同时:

System.out.println(curr.getSymbol(trLocale).substring(1));

For the mean time while you're waiting on Java:

System.out.println(curr.getSymbol(trLocale).substring(1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文