Browse Source

Merge pull request #2081 from alibaba/developing

修复导出浮点型数据可能精度异常的bug
pull/2082/head v3.0.0-beta3
Jiaju Zhuang 3 years ago committed by GitHub
parent
commit
088768f9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pom.xml
  2. 2
      src/main/java/com/alibaba/excel/converters/WriteConverterContext.java
  3. 4
      src/main/java/com/alibaba/excel/util/NumberUtils.java
  4. 29
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterTest.java
  5. 4
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>3.0.0-beta2</version> <version>3.0.0-beta3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

2
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.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* write converter context * write converter context
@ -12,6 +13,7 @@ import lombok.Data;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Data @Data
@NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class WriteConverterContext<T> { public class WriteConverterContext<T> {

4
src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -58,7 +58,7 @@ public class NumberUtils {
* @return * @return
*/ */
public static WriteCellData<?> formatToCellData(Number num, ExcelContentProperty contentProperty) { 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 if (contentProperty != null && contentProperty.getNumberFormatProperty() != null
&& StringUtils.isNotBlank(contentProperty.getNumberFormatProperty().getFormat())) { && StringUtils.isNotBlank(contentProperty.getNumberFormatProperty().getFormat())) {
WorkBookUtil.fillDataFormat(cellData, contentProperty.getNumberFormatProperty().getFormat(), null); WorkBookUtil.fillDataFormat(cellData, contentProperty.getNumberFormatProperty().getFormat(), null);
@ -134,7 +134,7 @@ public class NumberUtils {
if (!hasFormat(contentProperty)) { if (!hasFormat(contentProperty)) {
return new BigDecimal(string); return new BigDecimal(string);
} }
return BigDecimal.valueOf(parse(string, contentProperty).doubleValue()); return new BigDecimal(parse(string, contentProperty).toString());
} }
/** /**

29
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<Float> context = new WriteConverterContext<>();
context.setValue(95.62F);
WriteCellData<?> writeCellData = floatNumberConverter.convertToExcelData(context);
Assert.assertEquals(0, writeCellData.getNumberValue().compareTo(new BigDecimal("95.62")));
}
}

4
update.md

@ -1,3 +1,7 @@
# 3.0.0-beta3
* 修复导出浮点型数据可能精度异常的bug
# 3.0.0-beta2 # 3.0.0-beta2
* 优化写入样式 * 优化写入样式

Loading…
Cancel
Save