自定义法国数字格式

时间:2011-12-05 17:34:42

标签: java

我尝试为美国国家/地区设置自定义数字格式。它到目前为止工作正常。

    // Not something I want.
    NumberFormat numberFormat0 = NumberFormat.getNumberInstance(Locale.US);        
    System.out.println("US = " + numberFormat0.format(1234.0) + " (I wish to have 1,234.00)");
    System.out.println("US = " + numberFormat0.format(1234.567) + " (I wish to have 1,234.567)");
    System.out.println("US = " + numberFormat0.format(1234.5678) + " (I wish to have 1,234.568)\n");

    // Yes. Something I want :)
    NumberFormat goodNumberFormat = new DecimalFormat("#,##0.00#");
    System.out.println("US = " + goodNumberFormat.format(1234.0) + " (I wish to have 1,234.00)");
    System.out.println("US = " + goodNumberFormat.format(1234.567) + " (I wish to have 1,234.567)");
    System.out.println("US = " + goodNumberFormat.format(1234.5678) + " (I wish to have 1,234.568)\n");

这是输出。

US = 1,234 (I wish to have 1,234.00)
US = 1,234.567 (I wish to have 1,234.567)
US = 1,234.568 (I wish to have 1,234.568)

US = 1,234.00 (I wish to have 1,234.00)
US = 1,234.567 (I wish to have 1,234.567)
US = 1,234.568 (I wish to have 1,234.568)

然而,同样的事情对法国来说并不适用。因为,使用.,使用“空格”。

我写下面的代码。

    // Not something I want.
    NumberFormat numberFormat1 = NumberFormat.getNumberInstance(Locale.FRANCE);        
    System.out.println("FRANCE = " + numberFormat1.format(1234.0) + " (I wish to have 1 234,00)");
    System.out.println("FRANCE = " + numberFormat1.format(1234.567) + " (I wish to have 1 234,567)");
    System.out.println("FRANCE = " + numberFormat1.format(1234.5678) + " (I wish to have 1 234,567)\n");

    // Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "# ##0,00#"
    NumberFormat goodNumberFormat1 = new DecimalFormat("# ##0,00#");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.0) + " (I wish to have 1 234,00)");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.567) + " (I wish to have 1 234,567)");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.5678) + " (I wish to have 1 234,567)\n");

我收到以下错误。

FRANCE = 1 234 (I wish to have 1 234,00)
FRANCE = 1 234,567 (I wish to have 1 234,567)
FRANCE = 1 234,568 (I wish to have 1 234,567)
Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "# ##0,00#"

我能做些什么,拥有上述自定义数字格式?

2 个答案:

答案 0 :(得分:3)

    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.FRANCE);
    NumberFormat goodNumberFormat = new DecimalFormat("#,##0.00#", dfs);

答案 1 :(得分:2)

Taddaaa!

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator(' ');
NumberFormat goodNumberFormat1 = new DecimalFormat("#,##0.00#", symbols);
System.out.println("FRANCE = " + goodNumberFormat1.format(1234.0) + " (I wish to have 1 234,00)");
System.out.println("FRANCE = " + goodNumberFormat1.format(1234.567) + " (I wish to have 1 234,567)");
System.out.println("FRANCE = " + goodNumberFormat1.format(1234.5678) + " (I wish to have 1 234,567)\n");