Change dot to comma at DecimalFormat

kamaci picture kamaci · Oct 1, 2014 · Viewed 9.1k times · Source

When I use this formatter:

java.text.DecimalFormat("#.000");

for 1234567/1000000.0

output is:

1.235

I want it as like that:

1,235

and tried that:

java.text.DecimalFormat("#,000");

but it does not work as excepted. How to change dot to comma for my situation?

Answer

A4L picture A4L · Oct 1, 2014

You need to set the format symbols using an instance of DecimalFormatSymbols:

public void testDec() {
    DecimalFormat df = new DecimalFormat("#.000");
    DecimalFormatSymbols sym = DecimalFormatSymbols.getInstance();
    sym.setDecimalSeparator(',');
    df.setDecimalFormatSymbols(sym);
    System.out.println(df.format(1234567/1000000.0));
}

Output is

1,235