0

I am using apache poi 3.10-FINAL version to read a couple of cell values and multiply.

I am multiplying two cells, each with value 38.15 & 199.1, but the result is 7595.664999999999 instead of 7595.67

Is there a fix for this?

        is = new FileInputStream("V5.xlsx");
        XSSFWorkbook  wb = new XSSFWorkbook(is);
        XSSFDataFormat format = wb.createDataFormat();
        XSSFSheet  sheet = wb.getSheet("Sample Test");
        CreationHelper helper = wb.getCreationHelper();
        FormulaEvaluator formulaEval = helper.createFormulaEvaluator(); 

        Cell cell1 = sheet.getRow(2).getCell(1);
        Cell cell2 = sheet.getRow(2).getCell(2);
        cell1.setCellValue("199.1");
        cell2.setCellValue("38.15");

        sheet.getRow(2).getCell(3).setCellFormula("B3*C3");

        System.out.println(formulaEval.evaluate(sheet.getRow(2).getCell(3)));
user915534
  • 39
  • 1
  • 4
  • 1
    set the cells format – Scary Wombat Aug 14 '14 at 02:52
  • I had tried that, doesnt work. CreationHelper helper = wb.getCreationHelper(); FormulaEvaluator formulaEval = helper.createFormulaEvaluator(); sheet.getRow(2).getCell(2).setCellType(0); sheet.getRow(2).getCell(1).setCellType(0); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( helper.createDataFormat().getFormat("0.0")); cell1.setCellStyle(style); – user915534 Aug 14 '14 at 14:51

1 Answers1

0

The issue is with

Package: org.apache.poi.ss.formula.eval Class: TwoOperandNumericOperation

public static final Function MultiplyEval = new TwoOperandNumericOperation() {
        protected double evaluate(double d0, double d1) {
            return d0*d1;
        }
    };
user915534
  • 39
  • 1
  • 4