电子商务网站建设的发展趋势,wordpress添加分类图片尺寸,瑞昌建站公司,深圳交易平台网站开发文章目录 一、读取数字多了很多小数位的精度问题 一、读取数字多了很多小数位的精度问题
浮点型转成BigDecimal的时候会出现精度问题#xff0c;例如 这儿设置的实体类对象类型是String#xff0c;默认用到的是StringNumberConverter转换器 2.1.4 版本
public class Strin… 文章目录 一、读取数字多了很多小数位的精度问题 一、读取数字多了很多小数位的精度问题
浮点型转成BigDecimal的时候会出现精度问题例如 这儿设置的实体类对象类型是String默认用到的是StringNumberConverter转换器 2.1.4 版本
public class StringNumberConverter implements ConverterString {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 ConverterString {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 解决读取不准确的问题