Excel07SaxReader中,对于小数类型,增加精度判断(issue#IB0EJ9@Gitee)

This commit is contained in:
Looly 2024-11-19 16:42:29 +08:00
parent 812c75158e
commit 4437b63ee5
4 changed files with 38 additions and 3 deletions

View File

@ -2,12 +2,13 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.34(2024-11-15)
# 5.8.34(2024-11-19)
### 🐣新特性
* 【http 】 增加Windows微信浏览器识别issue#IB3SJF@Gitee
* 【core 】 ZipUtil.unzip增加编码容错issue#I3UZ28@Gitee
* 【core 】 Calculator兼容`x`字符作为乘号issue#3787@Github
* 【poi 】 Excel07SaxReader中对于小数类型增加精度判断issue#IB0EJ9@Gitee
### 🐞Bug修复
* 【core 】 修复DateUtil.rangeToList中step小于等于0时无限循环问题issue#3783@Github

View File

@ -0,0 +1,25 @@
package cn.hutool.json;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Issue3790Test {
@Test
void bigDecimalToStringTest() {
BigDecimal bigDecimal = new BigDecimal("0.01");
bigDecimal = bigDecimal.setScale(4, RoundingMode.HALF_UP);
Dto dto = new Dto();
dto.remain = bigDecimal;
final String jsonStr = JSONUtil.toJsonStr(dto, JSONConfig.create().setStripTrailingZeros(false));
Assertions.assertEquals("{\"remain\":0.0100}", jsonStr);
}
static class Dto {
public BigDecimal remain;
}
}

View File

@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.DependencyException;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelDateUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
@ -264,7 +265,15 @@ public class ExcelSaxUtil {
if (StrUtil.isBlank(value)) {
return null;
}
return getNumberValue(Double.parseDouble(value), numFmtString);
// issue#IB0EJ9 可能精度丢失
final double number = Double.parseDouble(value);
if(false == value.equals(Double.toString(number))){
// 精度丢失
return NumberUtil.toBigDecimal(value);
}
return getNumberValue(number, numFmtString);
}
/**

View File

@ -9,7 +9,7 @@ public class IssueIB0EJ9Test {
@Test
@Disabled
void saxReadTest() {
ExcelUtil.readBySax(FileUtil.file("d:/test/bbb.xlsx"), "Sheet1",
ExcelUtil.readBySax(FileUtil.file("d:/test/数值型测试.xlsx"), "hcm工资表",
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
}
}