我有以下代码:
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 );
这会产生输出:
raw value: 1713.6
formatted: $1,713
在Excel Professional 2010中打开文件时,该值显示为$ 1,714
为什么POI会产生与Excel不同的价值?有没有办法配置POI以便它返回相同的值(即遵循与Excel相同的舍入算法)?
我正在使用带有xls(Excel 97-2013)文件的POI 3.12。
编辑:此单元格的java格式为$#,##0
(POI内部)。以下是Excel显示的内容:
编辑2:也许这是Java的问题,而不是POI。以下代码:
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 ) );
产生结果:
double : 1713.6
HALF_DOWN: 1714
HALF_UP : 1713
为什么HALF_DOWN会围绕向上但是HALF_UP围绕向下?