{0}和{0,数字,整数}之间有什么区别

时间:2012-10-02 18:11:27

标签: java localization number-formatting messageformat

我正在尝试更多地了解Java的MessageFormat实用程序,在我们的代码库和其他地方的示例中,我看到{0}{0,number,integer}都用于数字,但我是不确定哪一种更好。

快速测试打印差异:

import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class MessageFormatTest
{
    public static void main(String[] args){
        MessageFormat simpleChoiceTest = new MessageFormat("{0}");
        MessageFormat explicitChoiceTest = new MessageFormat("{0,number,integer}");
        int[] set = new int[]{0,1,4,5,6,10,10000,24345};
        Locale[] locs = new Locale[]{Locale.US,Locale.UK,Locale.FRANCE,Locale.GERMANY};
        for(Locale loc : locs){
            simpleChoiceTest.setLocale(loc);
            explicitChoiceTest.setLocale(loc);
            for(int i : set){
                String simple = simpleChoiceTest.format(new Object[]{i});
                String explicit = explicitChoiceTest.format(new Object[]{i});
                if(!simple.equals(explicit)){
                    System.out.println(loc+" - "+i+":\t"+simple+
                        "\t"+NumberFormat.getInstance(loc).format(i));
                    System.out.println(loc+" - "+i+":\t"+explicit+
                        "\t"+NumberFormat.getIntegerInstance(loc).format(i));
                }
            }
        }
    }
}

输出:

fr_FR - 10000:  10 000  10 000
fr_FR - 10000:  10,000  10 000
fr_FR - 24345:  24 345  24 345
fr_FR - 24345:  24,345  24 345
de_DE - 10000:  10.000  10.000
de_DE - 10000:  10,000  10.000
de_DE - 24345:  24.345  24.345
de_DE - 24345:  24,345  24.345

令我感到惊讶的是,如果有任何我希望{0}不对该号码做任何事情,并且{0,number,integer}正确地将其本地化。相反,两者都被本地化,但似乎显式形式总是使用en_US本地化。

根据链接的文档,{0}通过NumberFormat.getInstance(getLocale()),而显式表单使用NumberFormat.getIntegerInstance(getLocale())。然而,当我直接调用它们(输出中的最后一列)时,两者看起来都相同,并且都正确地进行了本地化。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

你是对的。当您使用“MessageFormat(”{0,number,integer}“)”时,格式化程序在初始化时使用默认语言环境(en_US),并且数字被标记为在默认语言环境(en_US)中使用整数格式,因为下面的代码被执行在初始化时间本身。

// this method is internally called at the time of initialization
MessageFormat.makeFormat()
// line below uses default locale if locale is not
// supplied at initialization (constructor argument) 
newFormat = NumberFormat.getIntegerInstance(locale);

由于您之后设置了区域设置,因此对分配给数字的格式模式没有影响。如果您想以数字格式使用所需的语言环境,请在初始化时使用locale参数,例如:下面:

MessageFormat test = new MessageFormat("{0,number,integer}", Locale.FRANCE);

答案 1 :(得分:0)

在我看来,这是一个Java错误(界面错误)或文档问题。您应该在Oracle上打开一个新问题来解决这个问题。

正如Yogendra Singh所说,格式化程序(DecimalFormat)的实例是在MessageFormat构造函数时创建的。

MessageFormat simpleChoiceTest = new MessageFormat("{0}");
System.out.println(simpleChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints null
MessageFormat explicitChoiceTest = new MessageFormat("{0,number,currency}");
System.out.println(explicitChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints java.text.DecimalFormat@67500

调用MessageFormat.setLocale时,它不会更改其内部格式化程序的语言环境。

至少应更改文档以反映此问题。

那是我的java版本: java版“1.7.0_07” Java(TM)SE运行时环境(版本1.7.0_07-b11)