getting incorrect value from excel using apache poi

1.6k views Asked by At

in excel the value of column is 10101101010000000000 but when im reading it in java using POI the value is changed to 10101101009999999000, can anyone give me an idea on whats going on and how can i get the exact values from the excel. i've tried setting the celltype as string and use cell.getStringCellValue() and also this new BigDecimal(cell.getNumericCellValue()).toPlainString() but im still not getting the same value as with the excel

here's my code

List<BankClassVo> data = new ArrayList<BankClassVo>();
    FileInputStream fis = new FileInputStream(new File(Constant.VALIDATION_REFERENCE_FILE_PATH + Constant.BANK_CLASSIFICATION_REF_FILE + ".xlsx"));
    XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
    XSSFSheet mySheet = myWorkBook.getSheetAt(1);
    Iterator<Row> rowIterator = mySheet.iterator();


    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();

        BankClassVo vo = new BankClassVo ();


            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getColumnIndex() == 0) {
                    vo.setsClass(new BigDecimal(cell.getNumericCellValue()).toPlainString());
                }
                else if (cell.getColumnIndex() == 1) {
                    vo.setClassification(cell.getStringCellValue());
                }
            }

        data.add(vo);
    }

    myWorkBook.close();
    return data;
1

There are 1 answers

0
Zaw Than oo On BEST ANSWER

Use MathContext and RoundingMode. Ref

BigDecimal value = new BigDecimal(cell.getNumericCellValue(), new MathContext(10 , RoundingMode.CEILING));
System.out.println(value.toPlainString());