java.util.locale类似的类,了解java.util.Locale的奇怪输出

I had a perception that Locale is just about adding comma at proper positions at least in case of numbers. But I see a different output for what I have tried.

I tried the following,

public static void main(String[] args) {

DecimalFormat df = null;

df = (DecimalFormat) DecimalFormat.getInstance(Locale.CHINESE);

System.out.println("Locale.CHINESE "+df.format(12345.45));

df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);

System.out.println("Locale.GERMAN "+df.format(12345.45));

}

Output:

Locale.CHINESE 12,345.45

Locale.GERMAN 12.345,45

If you carefully look at the comma's, you'll see a major difference.

... An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to

tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number

should be formatted according to

the customs/conventions of the user's native country, region, or culture ...

I see a comma being interpreted as decimal point in another Locale, which is really a curious thing, as the value is being changed.

So, help me understand this. What exactly is Locale? Won't the drastic change in output cause major issue in code/data?

解决方案

I had a perception that Locale is just about adding comma at proper positions at least in case of numbers.

No, it affects the symbols used as well, as you've seen.

So, help me understand this. What exactly is Locale? Won't the drastic change in output cause major issue in code/data?

Only if you don't use them correctly :) Machine-to-machine communication should usually not be localized; typically if you really need to use text, it's best to use US as a reasonably invariant locale.

See DecimalFormatSymbols for more details of what is locale-specific.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值