mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
fix float bug
This commit is contained in:
parent
b5145a0bac
commit
838a2c45a6
@ -2673,6 +2673,22 @@ public class NumberUtil {
|
||||
return Calculator.conversion(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Number值转换为double<br>
|
||||
* float强制转换存在精度问题,此方法避免精度丢失
|
||||
*
|
||||
* @param value 被转换的float值
|
||||
* @return double值
|
||||
* @since 5.7.8
|
||||
*/
|
||||
public static double toDouble(Number value) {
|
||||
if(value instanceof Float){
|
||||
return Double.parseDouble(value.toString());
|
||||
}else{
|
||||
return value.doubleValue();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------- Private method start
|
||||
private static int mathSubNode(int selectNum, int minNum) {
|
||||
if (selectNum == minNum) {
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user