问题:POI读取Excel数值单元格时,读取的小数数值与真实值不一致

话不多说,直接上代码!

public static String getRealStringValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}else {
final CellType cellType = cell.getCellType();
if (CellType.NUMERIC.equals(cellType)) {
cellValue = DoubleUtils.realStringValueOfDouble(cell.getNumericCellValue());
} else {
cell.setCellType(CellType.STRING);
cellValue = cell.getStringCellValue();
}
return cellValue;
}
}
public class DoubleUtils {

    private DoubleUtils() {
} public static String realStringValueOfDouble(Double d) {
String doubleStr = d.toString();
boolean b = doubleStr.contains("E");
int indexOfPoint = doubleStr.indexOf('.');
if (b) {
int indexOfE = doubleStr.indexOf('E');
BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint + BigInteger.ONE.intValue(), indexOfE));
int pow = Integer.parseInt(doubleStr.substring(indexOfE + BigInteger.ONE.intValue()));
int xsLen = xs.toByteArray().length;
int scale = xsLen - pow > 0 ? xsLen - pow : 0;
final String format = "%." + scale + "f";
doubleStr = String.format(format, d);
} else {
java.util.regex.Pattern p = Pattern.compile(".0$");
java.util.regex.Matcher m = p.matcher(doubleStr);
if (m.find()) {
doubleStr = doubleStr.replace(".0", "");
}
}
return doubleStr;
}
}

POI读入Excel用String读取数值类型失真问题(精度丢失)的更多相关文章

  1. java使用POI实现excel文件的读取,兼容后缀名xls和xlsx

    需要用的jar包如下: 如果是maven管理的项目,添加依赖如下: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...

  2. C++11中string与数值类型的转换

    C++中string与数值类型的相互转换记录 string转int.double.long string s = "123.456"; // string -> int co ...

  3. c# float类型和double类型相乘出现精度丢失

    c# float类型和double类型相乘出现精度丢失 double db = 4.0; double db2 = 1.3; float f = 1.3F; float f2 = 4.0F; Deci ...

  4. POI读写Excel简述之读取

    一.POI读取Excel文件(以Excel2003版为例,2007版就是根据文件扩展名xlsx将HSSFWorkbook换为XSSFWorkbook,及其Sheet.Row.Cell也相应替换) // ...

  5. POI读入excel文件到Java中

    package Poi_Test; //导入java自带的包 import java.io.BufferedInputStream; import java.io.File; import java. ...

  6. float类型进行计算精度丢失的问题

    今天一个案子,用户反映数量差异明明是 2.0-1.8,显示的结果却为0.20000005,就自己写了段方法测试了一下:package test1;public class Test2 {/*** @p ...

  7. Java Float类型 减法运算时精度丢失问题

    package test1; public class Test2 { /*** @param args*/public static void main(String[] args) {   Flo ...

  8. Long类型数据前端精度丢失

    问题描述 后端把Long类型的数据传给前端,前端可能会出现精度丢失的情况.例如:201511200001725439这样一个Long类型的整数,传给前端后会变成201511200001725440 相 ...

  9. springboot中关于Long类型返回前端精度丢失问题处理

    使用了HuTool这个雪花算法后,会出现丢失精度的问题 hutool算法使用地址 对于一些大的业务表,自增主键这里 接口层得注意下是否会产生大数值 设计接口的时候采用String类型. 在项目中,我们 ...

随机推荐

  1. OkHttp3使用教程,实现get、post请求发送,自动重试,打印响应日志。

    一.创建线程安全的okhttp单例 import service.NetworkIntercepter;import service.RetryIntercepter;import okhttp3.* ...

  2. SpringCloud-Hystrix Dashboard 之 Unable to connect to Command Metric Stream

    实践hystrix dashboard仪表盘的时候,不管是按照书上的还是网上的,都提示Unable to connect to Command Metric Stream. 查了好久发现,如果使用sp ...

  3. 对象实例Vue

    var vm = new Vue({ el:'#app', data:{}, //数据 methods:{}, //方法调用 filters:{}, //私有过滤器 directives:{}, // ...

  4. easyui-datetimebox 控件绑定双击事件实现自动选中当前日期时间

    本方法是在不改变原 js 的情况下,通过扩展方法来实现本目的 首先在 datetimebox 控件中扩展一个 绑定双击事件 的方法 $.extend($.fn.datetimebox.methods, ...

  5. 数据分析--Matplotlib的基本使用

    一.Matplotlib概述 1.Matplotlib是一个强大的Python绘图和数据可视化的工具包. 2.python中操作方式: 安装方法:pip install matplotlib 引用方法 ...

  6. json入门初体验

    json是JavaScript对象表示法,也是轻量级的文本数据交互格式,独立于语言,能够自我描述 json文本格式在语法上与创建JavaScript对象代码相同,多以json不需要解析器,js程序能够 ...

  7. Spring MVC 梳理 - 四种HandlerMapping

    总结 (1)SpringMVC导入jar包,配置web.xml 这些都是基础的操作. (2)四种HandlerMapping:DefaultAnnotationHandlerMapping;Simpl ...

  8. Spring boot 梳理 - @Conditional

    @Conditional(TestCondition.class) 这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置. spring框架还 ...

  9. 在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射

    一.前言 在实际项目开发过程中,我们使用到的各种 ORM 组件都可以很便捷的将我们获取到的数据绑定到对应的 List<T> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能 ...

  10. ELK 学习笔记之 elasticsearch 基本查询

    elasticsearch 基本查询: 基本查询: term查询: terms查询: from和size查询: match查询: match_all查询: match_phrase查询: multi_ ...