简体   繁体   中英

Change dot to comma at DecimalFormat

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?

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

You can replace . with ,

DecimalFormat decimalFormat=new DecimalFormat("#.000");
double d=1234567/1000000.0;
System.out.println(decimalFormat.format(d).replace(".",","));

Out put:

1,235

Is there a particular reason your wanting to display a decimal number with a ","?

Otherwise

Double answer = 1234567/1000000.0;
DecimalFormat formatter = new DecimalFormat("#,###");
System.out.println(formatter.format(answer*1000));

Hope below code helps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM