Reputation: 42768
I try to have customized number format for US country. It works fine so far.
// 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");
Here is the output.
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)
However, same things doesn't work well for France. As they are using ,
for .
, and "space" for ,
.
I write the following code.
// 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");
I am getting the following error.
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#"
Is there anything I can do, to have the above customized number format?
Upvotes: 4
Views: 10770
Reputation: 109603
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.FRANCE);
NumberFormat goodNumberFormat = new DecimalFormat("#,##0.00#", dfs);
Upvotes: 4
Reputation: 62459
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");
Upvotes: 2