4
我嘗試爲美國國家定製數字格式。到目前爲止它工作正常。定製的法國數字格式
// 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#"
有什麼我可以做的,有上述定製的數字格式?
哦。我正在尋找具有數千個分隔符的「空間」。無論如何,你提出的解決方案是非常好的:) –
但我很好奇。如果德國人或法國人需要輸入「一點二」,他們是否在文本字段中鍵入「1,2」或「1.2」?將Integer.parseInt知道如何處理「1,2」? –
@Ying Cheng CHEOK:我的解決方案適合您嗎? – Tudor