了解java.util.Locale的奇怪输出 [英] Understanding the strange output of java.util.Locale

查看:151
本文介绍了了解java.util.Locale的奇怪输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个感觉, Locale 就是在适当的位置加上逗号,至少在数字的情况下。但是我看到了不同的输出,我试过了。



我尝试了以下内容:

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

$ / code $ / pre
$ b $ h3 $输出:

  Locale.CHINESE 12,345.45 
Locale.GERMAN 12.345,45

如果你仔细看看逗号,你会看到一个重要的区别。



现在, javadoc for java.util.Locale says


...一个需要Locale执行任务的操作称为locale-sensitive,并使用Locale对
定制用户信息。例如,显示一个数字是一个语言环境敏感的操作 - 数字
应该根据
来格式化用户所在国家,地区或文化的惯例/习惯...


我在另一个 Locale 中看到一个逗号被解释为小数点,这真的好奇东西,价值正在被改变。

那么,帮我理解一下。什么是 Locale ?输出的剧烈变化不会导致代码/数据的主要问题吗?

解决方案


一个看法是Locale只是在适当的位置加上逗号,至少在数字的情况下。

不,它会影响用作好吧,就像你看到的那样。


那么,帮我理解一下。 Locale究竟是什么?输出的剧烈变化不会导致代码/数据的主要问题吗?

只有当你不正确使用它们时:)机器间通信通常不应该被本地化;通常如果你真的需要使用文本,最好使用美国作为一个合理地不变的地方。



DecimalFormatSymbols 了解更多有关语言环境的详细信息。


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.

Now, the javadoc for java.util.Locale says

... 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.

这篇关于了解java.util.Locale的奇怪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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