Change Double format - dot to comma

1.1k Views Asked by At

I must change Double format from dot to comma. I try this:

DecimalFormat df = new DecimalFormat("#,00",
                   DecimalFormatSymbols.getInstance(Locale.GERMANY)); 
selectedSheet.addCell(new Number(selectedCellColumn, 
                                 selectedCellRow,
                                 Double.valueOf(df.format(value)));

but it`s not working. Have you got any ideas how you can change a dot to a comma?

4

There are 4 best solutions below

0
kajacx On

new Number() - I assume this takes double as the third argument? In that case, formatting the double value before you pass it in there is impossible, you need to set the format on how the sheet will display numbers in the cell formatting settings.

Or is the problem that you have a string like "5,3" and want to convert it to double? It looks like the varaible value already has a double value in it.

3
Sunchezz On

For (i guess) ApachePOI library to set a different CellUnitStyle use this:

CellStyle unitStyle = workbook.createCellStyle();
unitStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("#,##0.00"));
cell.setCellValue(value);
cell.setCellStyle(unit);

Following Formats are builtin available: ApachePOI builtin Formats

0
Lino On

You have a symbol table in the java doc of DecimalFormat:

Symbol   Location    Localized?    Meaning
------------------------------------------
0        Number      Yes           Digit
#        Number      Yes           Digit, zero shows as absent
.        Number      Yes           Decimal separator or monetary decimal separator
-        Number      Yes           Minus sign
,        Number      Yes           Grouping separator
etc...

You were using the grouping separator , but you wanted to use the decimal separator ., so change your string from #,00 to #.00:

DecimalFormat df = new DecimalFormat("#.00", DecimalFormatSymbols.getInstance(Locale.GERMANY));
String format = df.format(3.23456);
System.out.println(format); // prints 3,23
0
Anish B. On

Code :

double value = 123.12345;
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.GERMANY);
decimalFormatSymbols.setDecimalSeparator(',');
DecimalFormat df = new DecimalFormat("#.###", decimalFormatSymbols);
System.out.println(df.format(value));
selectedSheet.addCell(new Number(selectedCellColumn, selectedCellRow, Double.valueOf(df.format(value)));

Output :

123,123