简体   繁体   中英

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?

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

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.

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

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

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