简体   繁体   English

更改双精度格式-点到逗号

[英]Change Double format - dot to comma

I must change Double format from dot to comma. 我必须将Double格式从点更改为逗号。 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: 对于(我猜)ApachePOI库来设置不同的CellUnitStyle,请使用以下命令:

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 内置了以下格式: ApachePOI内置格式

new Number() - I assume this takes double as the third argument? new Number() -我假设这将double作为第三个参数? 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? 还是问题是您有一个字符串,例如“ 5,3”,并希望将其转换为double? It looks like the varaible value already has a double value in it. 变量value似乎已经包含一个双精度value

You have a symbol table in the java doc of DecimalFormat : 您在DecimalFormat的Java文档中有一个符号表:

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 : ,因此将您的字符串从#,00更改为#.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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