带有货币符号的Java String.format [英] Java String.format with currency symbol
问题描述
以下格式的现有代码用于格式数值:
There is some existing code of the follow form which is used for format numerical values:
String.format( pattern, value )
请注意,我无法更改代码本身 - 我只能更改提供给代码的格式模式。
Note that I cannot change the code itself - I can only change the format pattern supplied to the code.
输出默认语言环境的货币符号的格式模式是什么?基本上,我想实现以下输出:
What is the format pattern to output a currency symbol for the default locale? Essentially, I want to achieve the following output:
String.format( "...", 123 ) => $ 123
推荐答案
考虑到你给出的限制,我认为实现它是不可能的。
要获取当前Locale的货币符号,您需要至少代码。
With the constraints you have given, I think it is impossible to achieve it. To get to the current Locale's currency symbol, you'll need a minimum of code.
如果你绝对没有办法将代码添加到程序中,你最好使用已建立的符号货币(¤)。它是为了这个目的而建立的,用于表示没有任何更具体的符号的货币。
If you have absolutely no means to add code to the program, you'd best use the established symbol for "currency" (¤). It was established for this exact purpose, to symbolize currency absent any more specific symbol.
如果你不能改变给定的代码,但是将代码添加到项目中整体而言,您可以使用它来找出最佳使用的符号。获得后,您可以使用它为现有代码创建用于格式化的模式。
If you can't change the given code, but add code to the project as a whole, you can use that to find out the symbol best used. Once you have it, you can use it to create a pattern for the existing code to use for formatting.
如果您可以找到原始程序将在下一个程序中运行的Locale,您可以编写一个使用该设置填充配置的助手程序。
If you can find out in which Locale the original program will run in next, you could write a assistant program that uses that setting to fill your configuration.
这篇关于带有货币符号的Java String.format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!