Java:货币到区域设置映射可能吗? [英] Java: Currency to Locale Mapping Possible?

查看:126
本文介绍了Java:货币到区域设置映射可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在与金额相关的数据库中的值,比如说10.0。我也可以访问货币/ CurrencyCode。如何在不知道Locale的情况下使用NumberFormat / DecimalFormat /(other?)来格式化?根据文档,它将选择一个默认的语言环境,这将不会与一个外币工作。

解决方案

正确的行为说的是在用户的首选语言环境中格式化数量,而不是货币的典型语言环境。在客户端,您将拥有用户的偏好(Locale.getDefault());如果您在Web服务器端进行了一些操作,请使用Accept-Language或者最好是页面内容的语言环境来获取适当的语言环境。



推理是这样的:
英美用户将理解10,000,000欧元15,但不适用于德国等值,10.000.000,15欧元。

包含足够的信息来推断一个合适的地区,无论如何。


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.

解决方案

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.

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

The currency itself doesn't contain enough information to infer a suitable locale, anyway.

这篇关于Java:货币到区域设置映射可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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