fix float bug

This commit is contained in:
Looly
2021-08-09 21:40:17 +08:00
parent b5145a0bac
commit 838a2c45a6
3 changed files with 51 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.poi.excel.cell.setters;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
@@ -26,10 +27,6 @@ public class NumberCellSetter implements CellSetter {
public void setValue(Cell cell) {
// issue https://gitee.com/dromara/hutool/issues/I43U9G
// 避免float到double的精度问题
if (value instanceof Float) {
cell.setCellValue(value.floatValue());
} else {
cell.setCellValue(value.doubleValue());
}
cell.setCellValue(NumberUtil.toDouble(value));
}
}

View File

@@ -721,4 +721,16 @@ public class ExcelWriteTest {
writer.close();
}
@Test
@Ignore
public void writeFloatTest(){
//issue https://gitee.com/dromara/hutool/issues/I43U9G
String path = "d:/test/floatTest.xlsx";
FileUtil.del(path);
final ExcelWriter writer = ExcelUtil.getWriter(path);
writer.writeRow(ListUtil.of(22.9f));
writer.close();
}
}