Java: Currency to Locale Mapping Possible?

后端 未结 4 1201
Happy的楠姐
Happy的楠姐 2020-12-16 12:11

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?)

4条回答
  •  甜味超标
    2020-12-16 13:09

    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.

提交回复
热议问题