I'm trying to have numbers display as percentages in my cell. So far the calculations seems to all be working but I cannot get the numbers displayed to be formatted as percentages I've tried two different ways so far.
CellValue cv = evaluator.evaluate(cell);
DecimalFormat df = new DecimalFormat("###.##%");
cell.setCellValue(df.format(Math.abs(cell.getNumericCellValue())));
This way basically evaluates the cell and then gives it a java decimalFormat
and uses java to create the formatting and the just places the result into the cell. The second method uses a CellStyle
to try and attempt it and it looks like this
CellStyle stylePercent = wb.createCellStyle();
stylePercent.setDataFormat(wb.createDataFormat().getFormat("0.000%"));
where I then places the values into the sheet and then apply the style to the cell.
Neither of these have worked for me always leaving the value of 0.5 as 0.5 instead of 50% or even giving it 0.5%, it always just remains the same as without the styling.
This is also how I am applying the style and my formula as well.
cell.setCellFormula(""+cellValue+"/(SUM(D"+(row.getRowNum()+1)+" + F"+(row.getRowNum()+1)+" + H"+(row.getRowNum()+1)+"+ J"+(row.getRowNum()+1)+" + L"+(row.getRowNum()+1)+"+ N"+(row.getRowNum()+1)+"+ P"+(row.getRowNum()+1)+"+ R"+(row.getRowNum()+1)+"+ T"+(row.getRowNum()+1)+"+ V"+(row.getRowNum()+1)+"+ X"+(row.getRowNum()+1)+")+ Z"+(row.getRowNum()+1)+")");
cell.setCellStyle(stylePercent);