文章目录
- 一、读取数字多了很多小数位的精度问题
一、读取数字多了很多小数位的精度问题
浮点型转成BigDecimal的时候会出现精度问题,例如
这儿设置的实体类对象类型是String,默认用到的是StringNumberConverter转换器
2.1.4 版本
public class StringNumberConverter implements Converter<String> {@Overridepublic Class supportJavaTypeKey() {return String.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.NUMBER;}@Overridepublic String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {// If there are "DateTimeFormat", read as dateif (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {return DateUtils.format(DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),contentProperty.getDateTimeFormatProperty().getFormat());}// If there are "NumberFormat", read as numberif (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {return NumberUtils.format(cellData.getNumberValue(), contentProperty);}// Excel defines formattingif (cellData.getDataFormat() != null) {if (DateUtil.isADateFormat(cellData.getDataFormat(), cellData.getDataFormatString())) {return DateUtils.format(DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),globalConfiguration.getUse1904windowing(), null));} else {// 直接返回NumberValue,对弈的类型为BigDecimal,因为BigDecimal由double转换而来// 出现了精度读取的问题,所以此时直接读取NumberValue,精度不准确的时候多出很多小数点// 这种情况不是必现的,613999.06是个例子return NumberUtils.format(cellData.getNumberValue(), contentProperty);}}// Default conversion numberreturn NumberUtils.format(cellData.getNumberValue(), contentProperty);}@Overridepublic CellData convertToExcelData(String value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return new CellData(new BigDecimal(value));}
}
2.2.8 版本
public class StringNumberConverter implements Converter<String> {@Overridepublic Class supportJavaTypeKey() {return String.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.NUMBER;}@Overridepublic String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {// If there are "DateTimeFormat", read as dateif (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {return DateUtils.format(DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),contentProperty.getDateTimeFormatProperty().getFormat());}// If there are "NumberFormat", read as numberif (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {return NumberUtils.format(cellData.getNumberValue(), contentProperty);}// Excel defines formattingif (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {// 直接返回doubleValue,对弈的类型为double,转成String不会出现精度问题return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),cellData.getDataFormatString(), globalConfiguration);}// Default conversion numberreturn NumberUtils.format(cellData.getNumberValue(), contentProperty);}@Overridepublic CellData convertToExcelData(String value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return new CellData(new BigDecimal(value));}
}
如果无法升级版本,可以重写转换器StringNumberConverter ,解决读取不准确的问题