xampo
xampo

Reputation: 399

Change Double format - dot to comma

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?

Upvotes: 0

Views: 1181

Answers (4)

Anish B.
Anish B.

Reputation: 16589

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

Upvotes: 0

Lino
Lino

Reputation: 19910

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

Upvotes: 1

Sunchezz
Sunchezz

Reputation: 778

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

Upvotes: 2

kajacx
kajacx

Reputation: 12959

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.

Upvotes: 1

Related Questions