]> source.dussan.org Git - poi.git/commitdiff
[github-321] Fix issue with rounding in DataFormatter. Thanks to Colin Wang. This...
authorPJ Fanning <fanningpj@apache.org>
Sat, 9 Apr 2022 12:02:55 +0000 (12:02 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 9 Apr 2022 12:02:55 +0000 (12:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899680 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
test-data/spreadsheet/github-321.xlsx

index 3a7fa17d30f1219879de9d16eb8947c822e18fdb..ab606f93dcd4a9cd2450ef4d4b1676fb58e5bf06 100644 (file)
@@ -1406,9 +1406,11 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
             DataFormatter dataFormatter = new DataFormatter();
             FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
             XSSFCell a3 = xssfSheet.getRow(2).getCell(0);
+            assertEquals(2.05, a3.getNumericCellValue());
             assertEquals("2.05", dataFormatter.formatCellValue(a3));
             assertEquals("2.05", dataFormatter.formatCellValue(a3, formulaEvaluator));
             XSSFCell a4 = xssfSheet.getRow(3).getCell(0);
+            assertEquals(2.05, a4.getNumericCellValue());
             assertEquals("2.1", dataFormatter.formatCellValue(a4));
             assertEquals("2.1", dataFormatter.formatCellValue(a4, formulaEvaluator));
         }
index 7a18d30e687fc912cb61322b78c6c26574006ba7..4b8a3b8689e13fea37deb96d7ebee354f2f28da7 100644 (file)
@@ -51,7 +51,7 @@ import org.apache.poi.util.LocaleUtil;
 
 
 /**
- * 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
  * need to display data exactly as it appears in Excel. Supported formats
  * include currency, SSN, percentages, decimals, dates, phone numbers, zip
@@ -139,7 +139,7 @@ public class DataFormatter {
     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,
      *  Magenta, Red, White, Yellow, "Color n" (1<=n<=56)
      */
@@ -948,9 +948,9 @@ public class DataFormatter {
         Format numberFormat = getFormat(cell, cfEvaluator);
         double d = cell.getNumericCellValue();
         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
     }
 
index 0b517e9eaa7e1888ee54f7e22d24ed81503ef8c8..2f50722c55bb6dc55f9ed00250283e0b83c6f38e 100644 (file)
Binary files a/test-data/spreadsheet/github-321.xlsx and b/test-data/spreadsheet/github-321.xlsx differ