title: Always show two decimal points in excel cells using Apache poi
body: <p>For example,</p>
  
  <pre><code>XSSFCellStyle style=(XSSFCellStyle) workbook.createCellStyle();
  style.setDataFormat(workbook.createDataFormat().getFormat("#.##"));
  
  productCell.setCellValue(12.4);
  productCell.setCellType(Cell.CELL_TYPE_NUMERIC);
  productCell.setCellStyle(style);
  </code></pre>
  
  <p>This displays <code>12.4</code> in the specified cell. It should be <code>12.40</code>. The value <code>12</code> is displayed as <code>12.</code> which is quite unnecessary.</p>
  
  <p>If the value is <code>0</code> then, it displays a dot <code>.</code>. It should always display two decimal digits - <code>0.00</code>, in this case regardless of the value being stored in a cell.</p>
  
  <p>How to force excel to always show two decimal digits in a numeric cell?</p>
  
  <hr>
  
  <p>I use one of the following styles to display numeric cells.</p>
  
  <pre><code>XSSFColor commonColor = new XSSFColor(new java.awt.Color(240, 240, 240));
  XSSFColor cellBorderColour = new XSSFColor(new java.awt.Color(0, 76, 153));
  
  Font font = workbook.createFont();
  font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  font.setColor(IndexedColors.DARK_BLUE.index);
  
  XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
  style.setFillForegroundColor(commonColor);
  style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  style.setAlignment(CellStyle.ALIGN_RIGHT);
  style.setFont(font);
  
  style.setBorderLeft(BorderStyle.HAIR);
  style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, cellBorderColour);
  style.setBorderTop(BorderStyle.HAIR);
  style.setBorderColor(XSSFCellBorder.BorderSide.TOP, cellBorderColour);
  style.setBorderRight(BorderStyle.HAIR);
  style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, cellBorderColour);
  style.setBorderBottom(BorderStyle.DOUBLE);
  style.setBottomBorderColor(cellBorderColour);
  </code></pre>
  
  <p>I applied some excel formulae to perform some calculations on numeric cells that are expected to be performed after applying a number format (rounding half up) on numeric cells.</p>