触发场景描述 用@NumberFormat注解转换double类型 触发Bug的代码
//表格映射对象double属性
/**
* 格式化百分比
*/
@NumberFormat("#.##%")
@ExcelProperty("百分比")
private Double num;
//读取excel的Listener
@Override
public void invoke(DemoData demoData, AnalysisContext analysisContext) {
int row = analysisContext.readRowHolder().getRowIndex();
int sheetNo = analysisContext.readSheetHolder().getSheetNo();
String sheetName = analysisContext.readSheetHolder().getSheetName();
log.info("读取到[{}]的第{}行记录:{}", sheetName, row, JSONObject.toJSONString(demoData));
}
提示的异常或者没有达到的效果
11:37:01.360 [main] INFO com.spring.component.easyexcel.listener.DemoDataListener - 读取到[Demo]的第9行记录:{"date":1589202943000,"no":"WEEK_3","num":0.6520999999999999,"zan":"点赞8"} 11:37:01.361 [main] INFO com.spring.component.easyexcel.listener.DemoDataListener - 读取到[Demo]的第10行记录:{"date":1589202943000,"no":"WEEK_4","num":0.74,"zan":"点赞9"}
建议 com.alibaba.excel.util.NumberUtils#parse 方法返回Number为BigDecimal对象,避免double、float等类型出现精度丢失的情况。 decimalFormat.setParseBigDecimal(true);