2017-11-13 18 views
1

私はapache.poiを使用してExcelから数値を読み取りました。apache.poi HSSFCell getNumericCellValue不正確な読み取り

hssfCell.getNumericCellValue() 

ただし、結果が間違っているExcelファイルの例です。

Example of excel file

セルN14の正しい結果が11115(または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を示します。どのようにしてこの価値を得ることができますか?

答えて

0

あなたはこれを使用することができます:

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

問題は精度がハードコードされていることです。小数点以下2桁のみを使用する場合、このソリューションはうまく動作します。

関連する問題