]> source.dussan.org Git - poi.git/commitdiff
[github-321] Fix issue with rounding in DataFormatter. First try broke a test.
authorPJ Fanning <fanningpj@apache.org>
Sat, 9 Apr 2022 13:55:25 +0000 (13:55 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 9 Apr 2022 13:55:25 +0000 (13:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899686 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java

index 78d58e7767873b83cc14a2061a0ecb5317dfa6d8..c7725d3818e8e3b716f30571cb25296fd548151b 100644 (file)
@@ -950,7 +950,14 @@ public class DataFormatter {
         if (numberFormat == null) {
             return Double.toString(d);
         }
-        String formatted = numberFormat.format(new BigDecimal(Double.toString(d)));
+        String formatted;
+        try {
+            //see https://github.com/apache/poi/pull/321 -- but this sometimes fails as Double.toString
+            //can produce strings that can't be parsed by BigDecimal
+            formatted = numberFormat.format(new BigDecimal(Double.toString(d)));
+        } catch (NumberFormatException nfe) {
+            formatted = numberFormat.format(d);
+        }
         return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
     }