zoukankan      html  css  js  c++  java
  • Java POI读取excel中数值精度损失

    描述:
    excel 单元格中,纯数字的单元格,读取后 后面会加上 .0 。
    例如: 1 --> 1.0

    而使用下面的方法,可能会对小数存在精度损失

    cell.setCellType(CellType.STRING); //读取前将单元格设置为文本类型读取
    

    例如: 2.2 -->  2.1999999997

    目前的解决办法:
    一. 将excel单元格改为文本类型。
    注意,直接修改单元格属性不管用, 使用 分列 的方式,可以实现将数值改为文本类型。

    二. java处理

    public class CommonUtil {
    
        private static NumberFormat numberFormat = NumberFormat.getNumberInstance();
    
        static {
            numberFormat.setGroupingUsed(false);
        }
    
        public static String getCellValue(Cell cell) {
            if (null == cell) {
                return "";
            }
            Object value;
            switch (cell.getCellTypeEnum()) {
                // 省略
                case NUMERIC:
                    double d = cell.getNumericCellValue();   
                    value = numberFormat.format(d);        // 关键在这里!
                //省略 
            }
            return value == null ? "" : value.toString();
        }
    }

    上面的方法可以获取一个正确的数值.

    相关链接:

    https://blog.csdn.net/qq_34531925/article/details/76735345

    https://www.cnblogs.com/magicya/p/12626377.html

  • 相关阅读:
    bash特性
    FHS 层级文件系统
    环境变量的问题
    linux认识
    搜索引擎的使用
    nginx
    部署操作手册
    git
    添加tag
    pycharm中使用git
  • 原文地址:https://www.cnblogs.com/Steven5007/p/12767153.html
Copyright © 2011-2022 走看看