apache.poi HSSFCell getNumericCellValue读取不正确

我使用apache.poi从excel中读取数值。

hssfCell.getNumericCellValue() 

但是这里是结果错误的excel文件的例子。

excel文件的例子

单元格N14的正确结果是11,115(或具有Excel格式的11,12)。 但hssfCell.getNumericCellValue()得到结果11.114999999999998。

这是公式单元格,所以我可以使用dateFormatter,那么结果是11,11。

 HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(importFile)); FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(wb); DataFormatter dataFormatter = new DataFormatter(); formulaEvaluator.evaluate(hssfCell); dataFormatter.formatCellValue(hssfCell, formulaEvaluator); 

Excel显示我11,12。 我怎样才能得到这个价值?

你可以使用这个:

 DecimalFormat decimalFormat = new DecimalFormat("#.##"); decimalFormat.setRoundingMode(RoundingMode.CEILING); decimalFormat.format(hssfCell.getNumericCellValue()); 

问题是精度是硬编码。 如果你只使用2位小数,这个解决scheme将正常工作。