我有打印计算货币价值的应用程序,我想显示默认货币格式值。
I have an app that prints a calculated money value and I want to display that value with the default currency format.
例如在欧洲你可以这样写:
For example in Europe you would write:
1.000,95€
在美国,我想你会写
1,000.95 $
1,000.95$
在其他货币的有或多或少的值显示为小数,在美国,它是2,但在日本,它是0。
In other currencies there are more or less values displayed for the decimal fraction, in US it would be 2 but in Japan it would be 0.
我怎样才能获得一个确切尽可能格式为所有现有货币?
How can I obtain a exact as possible format for all existing currencies?
我找到了解决办法。类的NumberFormat 有predefined格式化的,众说纷纭。还有一个用于格式化货币值。
I found the solution. THe class NumberFormat has a multitude of predefined formatters. There is also one for formatting currency Values.
如果您使用静态方法getCurrencyInstance类将返回一个格式化该设备的缺省货币。我用下面的code设置我的结果:
If you use the static method getCurrencyInstance the class will return a formatter for the device default currency. I use the following code to set my result:
NumberFormat format = NumberFormat.getCurrencyInstance();
((TextView) findViewById(R.id.text_result)).setText(format.format(result));