今日份鸡汤:不完美又何妨,万物皆有裂痕,那是光进来的地方~
问题描述:
我使用Apache POI读取excel表格,其中有一列数值为:1215515164,但是我Java后端debug的时候,看到解析出来的数据值为:1.215515164E9,而我的需求其实就是获取到 1215515164 这个值,那就需要想一下:有没有办法从原始Cell中获取值作为字符串?
解决办法:
使用BigDecimal的toPlainString()方法,它采用科学记数法并将其转换为相应的数字字符串值。
代码演示:
Row row = sheet.getRow(0);
Object o = getCellValue(row.getCell(0));
System.out.println(new BigDecimal(o.toString()).toPlainString());
public Object getCellValue(Cell cell) {
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case BOOLEAN:
return cell.getBooleanCellValue();
case NUMERIC:
return cell.getNumericCellValue();
case ERROR:
return cell.getErrorCellValue();
}
}
return null;
}
网友评论