How do I format a Currency for a Locale in Java

自古美人都是妖i 提交于 2019-12-23 10:17:24

问题


I'm surprised that I'm having real trouble finding how to do this. It seems to me it should be pretty straightforward. Perhaps I'm looking in the wrong place.

Suppose I have the following code:

double amount = 123.45D; //yes, I know that I should be using longs
Locale uk = Locale.UK;
Locale fr = Locale.FR;
Currency euro = Currency.getInstance("EUR");

How do I get instances of NumberFormat that let me print out the 123.45 value correctly formatted for the Locale? ie I want the following for the UK and France respectively :

€123.45
123,45 €

I can't use NumberFormat.getCurrencyInstance(Locale) as that will format the UK format as Sterling (£). What I am looking for is NumberFormat.getCurrencyInstance(Locale, Currency), but that doesn't seem to exist.

Any ideas?


回答1:


Use NumberFormat.setCurrency() to modify the currency used for a specific NumberFormat acquired via NumberFormat.getCurrencyInstance(Locale).



来源:https://stackoverflow.com/questions/5713171/how-do-i-format-a-currency-for-a-locale-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!