Browse Source

[github-321] Fix issue with rounding in DataFormatter. Thanks to Colin Wang. This closes #321

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899680 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_3
PJ Fanning 2 years ago
parent
commit
a8f1e7acd1

+ 2
- 0
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java View File

DataFormatter dataFormatter = new DataFormatter(); DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
XSSFCell a3 = xssfSheet.getRow(2).getCell(0); XSSFCell a3 = xssfSheet.getRow(2).getCell(0);
assertEquals(2.05, a3.getNumericCellValue());
assertEquals("2.05", dataFormatter.formatCellValue(a3)); assertEquals("2.05", dataFormatter.formatCellValue(a3));
assertEquals("2.05", dataFormatter.formatCellValue(a3, formulaEvaluator)); assertEquals("2.05", dataFormatter.formatCellValue(a3, formulaEvaluator));
XSSFCell a4 = xssfSheet.getRow(3).getCell(0); XSSFCell a4 = xssfSheet.getRow(3).getCell(0);
assertEquals(2.05, a4.getNumericCellValue());
assertEquals("2.1", dataFormatter.formatCellValue(a4)); assertEquals("2.1", dataFormatter.formatCellValue(a4));
assertEquals("2.1", dataFormatter.formatCellValue(a4, formulaEvaluator)); assertEquals("2.1", dataFormatter.formatCellValue(a4, formulaEvaluator));
} }

+ 4
- 4
poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java View File





/** /**
* DataFormatter contains methods for formatting the value stored in an
* DataFormatter contains methods for formatting the value stored in a
* Cell. This can be useful for reports and GUI presentations when you * Cell. This can be useful for reports and GUI presentations when you
* need to display data exactly as it appears in Excel. Supported formats * need to display data exactly as it appears in Excel. Supported formats
* include currency, SSN, percentages, decimals, dates, phone numbers, zip * include currency, SSN, percentages, decimals, dates, phone numbers, zip
private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+])"); private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+])");


/** /**
* A regex to match the colour formattings rules.
* A regex to match the colour formatting's rules.
* Allowed colours are: Black, Blue, Cyan, Green, * Allowed colours are: Black, Blue, Cyan, Green,
* Magenta, Red, White, Yellow, "Color n" (1<=n<=56) * Magenta, Red, White, Yellow, "Color n" (1<=n<=56)
*/ */
Format numberFormat = getFormat(cell, cfEvaluator); Format numberFormat = getFormat(cell, cfEvaluator);
double d = cell.getNumericCellValue(); double d = cell.getNumericCellValue();
if (numberFormat == null) { if (numberFormat == null) {
return String.valueOf(d);
return NumberToTextConverter.toText(d);
} }
String formatted = numberFormat.format(d);
String formatted = numberFormat.format(new BigDecimal(NumberToTextConverter.toText(d)));
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
} }



BIN
test-data/spreadsheet/github-321.xlsx View File


Loading…
Cancel
Save