Java:货币到语言环境的映射可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/758255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Currency to Locale Mapping Possible?
提问by Carlos S
I have a value stored in a DB correlating to a monetary amount, say 10.0. I also have access to the Currency/CurrencyCode. How can I use NumberFormat/DecimalFormat/(other?) to format when I don't know the Locale? According to the docs it will pick a default locale which won't work with a foreign Currency.
我在数据库中存储了一个与货币金额相关的值,例如 10.0。我还可以访问货币/货币代码。当我不知道语言环境时,如何使用 NumberFormat/DecimalFormat/(other?) 进行格式化?根据文档,它将选择一个不适用于外币的默认语言环境。
采纳答案by JasonTrue
The correct behavior, generally speaking, is to format the amount in the User's preferred locale, not the currency's typical locale. On the client side, you'll have the user's preference (Locale.getDefault()); if you are doing something on the web server side, use the Accept-Language or, preferably, the page content's locale to obtain the proper a locale.
一般来说,正确的行为是在用户的首选语言环境中格式化金额,而不是货币的典型语言环境。在客户端,您将拥有用户的首选项 (Locale.getDefault());如果您在 Web 服务器端执行某些操作,请使用 Accept-Language 或最好使用页面内容的语言环境来获取正确的语言环境。
The reasoning is this: An English-US user will understand 10,000,000.15 but not the suitable-for-Germany equivalent, 10.000.000,15
推理是这样的:英语-美国用户会理解 10,000,000.15 但不会理解适用于德国的等效值 10.000.000,15
The currency itself doesn't contain enough information to infer a suitable locale, anyway.
无论如何,货币本身不包含足够的信息来推断合适的语言环境。
回答by user761574
JasonTrue is correct, but you can override the currency of the NumberFormat's locale:
JasonTrue 是正确的,但您可以覆盖 NumberFormat 语言环境的货币:
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
//the user may have selected a different currency than the default for their locale
Currency currency = Currency.getInstance("GBP");
numberFormat.setCurrency(currency);
numberFormat.format(amount);
回答by erickson
What if the currency code is EUR? And, while it has taken a beating, USD is still used throughout the world. Inferring the locale from the currency code seems unreliable. Can you introduce an explicit user preference for the locale instead?
如果货币代码是 EUR 呢?而且,虽然它受到了打击,但美元仍在世界各地使用。从货币代码推断区域设置似乎不可靠。您能否为语言环境引入明确的用户偏好?
The information you are looking for is not part of the built-in Java currency database, so there is not an API for it. You could create your own tablefor the many cases that are unambiguous.
您要查找的信息不是内置 Java 货币数据库的一部分,因此没有针对它的 API。您可以为许多明确的情况创建自己的表。
回答by duffymo
I would say that if your database is storing a currency value it should be hanging onto the units at the same time. It sounds like you're doing that now. Can you add the Locale to the database at the same time? Could be a decent solution.
我会说,如果您的数据库正在存储货币价值,它应该同时挂在单位上。听起来你现在正在这样做。您可以同时将 Locale 添加到数据库中吗?可能是一个不错的解决方案。
回答by Lilo
Here's a way to determine the locale from the currency code
这是一种从货币代码确定语言环境的方法
let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(NSLocale.localeIdentifierFromComponents([NSLocaleCurrencyCode: currencyCode])))
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencyCode = currencyCode
formatter.locale = locale
formatter.maximumFractionDigits = 2
formatter.stringFromNumber(number)

