Apache POI percentage format not working

6.4k views Asked by At

I have a module where I have to take double values from html form and then populate them in an excel using Apache POI.

Excel cells are showing percentage. Module is not working. please see code

    CellStyle style = wb.createCellStyle();
    style.setDataFormat(wb.createDataFormat().getFormat("##.##%"));
    sheet.getRow(4).getCell(3).setCellValue(wifimodel.getMobileSubscriberCountToday());
    sheet.getRow(4).getCell(3).setCellStyle(style);

Now wifimodel.getMobileSubscriberCountToday() value is 10.0 and in excel it comes as 1000,%

please help

1

There are 1 answers

0
Gagravarr On

Promoting a comment to an answer

In maths equivalences, and in computers, percentages are stored as relative to 1 not 100. As wikipedia explains, that's because a percentage is a number or ratio expressed as a fraction of 100. As such, 10% is not stored as 10, but instead as 0.10. That means that to store your value of 10%, you need to put it in an Apache POI cell formatted as a percentage, with a numeric value of 0.10.

As you have found, putting in a value of 10 is treated as 10*100 = 1,000 %, which is 100 times what you had in mind!