diff --git a/pom.xml b/pom.xml index 2fb651aa..01134342 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 3.0.0-beta2 + 3.0.0-beta3 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java b/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java index 36ce56c6..cb01c773 100644 --- a/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java +++ b/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java @@ -5,6 +5,7 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; /** * write converter context @@ -12,6 +13,7 @@ import lombok.Data; * @author Jiaju Zhuang */ @Data +@NoArgsConstructor @AllArgsConstructor public class WriteConverterContext { diff --git a/src/main/java/com/alibaba/excel/util/NumberUtils.java b/src/main/java/com/alibaba/excel/util/NumberUtils.java index ea2635e6..4226e967 100644 --- a/src/main/java/com/alibaba/excel/util/NumberUtils.java +++ b/src/main/java/com/alibaba/excel/util/NumberUtils.java @@ -58,7 +58,7 @@ public class NumberUtils { * @return */ public static WriteCellData formatToCellData(Number num, ExcelContentProperty contentProperty) { - WriteCellData cellData = new WriteCellData<>(BigDecimal.valueOf(num.doubleValue())); + WriteCellData cellData = new WriteCellData<>(new BigDecimal(num.toString())); if (contentProperty != null && contentProperty.getNumberFormatProperty() != null && StringUtils.isNotBlank(contentProperty.getNumberFormatProperty().getFormat())) { WorkBookUtil.fillDataFormat(cellData, contentProperty.getNumberFormatProperty().getFormat(), null); @@ -134,7 +134,7 @@ public class NumberUtils { if (!hasFormat(contentProperty)) { return new BigDecimal(string); } - return BigDecimal.valueOf(parse(string, contentProperty).doubleValue()); + return new BigDecimal(parse(string, contentProperty).toString()); } /** diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterTest.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterTest.java new file mode 100644 index 00000000..05c91d13 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterTest.java @@ -0,0 +1,29 @@ +package com.alibaba.easyexcel.test.core.converter; + +import java.math.BigDecimal; + +import com.alibaba.excel.converters.WriteConverterContext; +import com.alibaba.excel.converters.floatconverter.FloatNumberConverter; +import com.alibaba.excel.metadata.data.WriteCellData; + +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +/** + * @author Jiaju Zhuang + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ConverterTest { + + @Test + public void t01FloatNumberConverter() { + FloatNumberConverter floatNumberConverter = new FloatNumberConverter(); + WriteConverterContext context = new WriteConverterContext<>(); + context.setValue(95.62F); + WriteCellData writeCellData = floatNumberConverter.convertToExcelData(context); + Assert.assertEquals(0, writeCellData.getNumberValue().compareTo(new BigDecimal("95.62"))); + } + +} diff --git a/update.md b/update.md index 088ee95e..7a2cb568 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,7 @@ +# 3.0.0-beta3 + +* 修复导出浮点型数据可能精度异常的bug + # 3.0.0-beta2 * 优化写入样式