如何在Java中格式化语言环境的货币 [英] How do I format a Currency for a Locale in Java

查看:113
本文介绍了如何在Java中格式化语言环境的货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令我感到惊讶的是,我在寻找如何执行此操作时遇到了真正的麻烦.在我看来,它应该非常简单.也许我在找错地方了.

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.

假设我有以下代码:

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");

如何获取NumberFormat的实例,这些实例可以让我打印出正确设置为Locale格式的123.45值?即我想分别为英国和法国提供以下内容:

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 €

我不能使用NumberFormat.getCurrencyInstance(Locale),因为那样会将英国格式格式化为英镑(£).我要查找的是NumberFormat.getCurrencyInstance(Locale, Currency),但似乎不存在.

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.

有什么想法吗?

推荐答案

使用

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

这篇关于如何在Java中格式化语言环境的货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆