Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@NumberFormat注解转换double时可能会丢失精度 #1306

Closed
jlzl123 opened this issue May 12, 2020 · 4 comments
Closed

@NumberFormat注解转换double时可能会丢失精度 #1306

jlzl123 opened this issue May 12, 2020 · 4 comments
Assignees
Labels
bug Something isn't working pending verification This problem needs to be confirmed

Comments

@jlzl123
Copy link

jlzl123 commented May 12, 2020

建议先去看文档
快速开始常见问题
触发场景描述
@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));
  }

提示的异常或者没有达到的效果
image

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);

@jlzl123 jlzl123 added the bug Something isn't working label May 12, 2020
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label May 14, 2020
@zhuangjiaju
Copy link
Collaborator

非常感谢您的意见,已经在最新版修复。

@jinze1107
Copy link

jinze1107 commented Jul 26, 2023

使用最版本 easyexcel 3.3.2 ,导出字段类型为 BigDecimal ,并使用 @numberformat("#.##%")格式化 导出, 当字段值为 0.0 时 会被format成 .% ; 这个问题应该没有被修复

@ChewJ404
Copy link

使用最版本 easyexcel 3.3.2 ,导出字段类型为 BigDecimal ,并使用 @numberformat("#.##%")格式化 导出, 当字段值为 0.0 时 会被format成 .% ; 这个问题应该没有被修复

你还没理解 format 用法
在注解中,需要指定数字的格式化规则,规则为字符串类型的参数。该参数应使用 # 和 0 两个字符来表示数字的占位符。

  • # 表示有数字的地方显示数字,没有数字的地方显示空格

  • 0 表示有数字的地方显示数字,没有数字的地方补零

具体格式化规则可以参考 Java 中的 DecimalFormat 类。

@ye406932993
Copy link

目前对 @numberformat("#,###.##") 中 ,如果是整数,还存在点的问题,请问有修复吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending verification This problem needs to be confirmed
Projects
None yet
Development

No branches or pull requests

5 participants