I have the following code:
Workbook workbook = WorkbookFactory.create( file );
Sheet sheet = workbook.getSheet( "Sheet1" );
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
DataFormatter formatter = new DataFormatter( true );
int rowNum = 0;
int colNum = 0;
Row row = sheet.getRow( rowNum );
Cell cell = row.getCell( colNum );
double rawCellValue = cell.getNumericCellValue();
System.out.println( "raw value: " + rawCellValue );
String cellValue = formatter.formatCellValue( cell, evaluator );
System.out.println( "formatted: " + cellValue );
This produces the output:
raw value: 1713.6
formatted: $1,713
When I open the file in Excel Professional 2010, the value displays as $1,714
Any ideas why POI produces a different value than Excel? Is there a way to configure POI so that it returns the same value (i.e. obeys the same rounding algorithm as Excel)?
I'm using POI 3.12 with a xls (Excel 97-2013) file.
EDIT: The java format for this cell is $#,##0
(internal to POI). Here's what Excel shows:
EDIT 2: Perhaps this is a problem with Java, not POI. The following code:
double number = 1713.6;
System.out.println( "double : " + number );
DecimalFormat format = new DecimalFormat("#0");
format.setRoundingMode( RoundingMode.HALF_DOWN );
System.out.println( "HALF_DOWN: " + format.format( number ) );
format.setRoundingMode( RoundingMode.HALF_UP );
System.out.println( "HALF_UP : " + format.format( number ) );
Produces the result:
double : 1713.6
HALF_DOWN: 1714
HALF_UP : 1713
Why would HALF_DOWN round up but HALF_UP round down?