Author: nick
Date: Sat Apr 11 15:36:27 2020
New Revision: 1876396

URL: http://svn.apache.org/viewvc?rev=1876396&view=rev
Log:
#64319 Tighten the scientific format code to avoid making eg TRUE into TRUE+, 
handle formats like 0E-0, and ensure formats like 0E0 work correctly

Modified:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java?rev=1876396&r1=1876395&r2=1876396&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java Sat Apr 
11 15:36:27 2020
@@ -686,7 +686,7 @@ public class DataFormatter implements Ob
                 i--;
 
             // for scientific/engineering notation
-            } else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') {
+            } else if ((c == '+' || c == '-') && i > 0 && sb.charAt(i - 1) == 
'E') {
                 sb.deleteCharAt(i);
                 i--;
             }
@@ -929,8 +929,12 @@ public class DataFormatter implements Ob
         else {
             result = numberFormat.format(new BigDecimal(textValue));
         }
-        // Complete scientific notation by adding the missing +.
-        if (result.indexOf('E') > -1 && !result.contains("E-")) {
+        
+        // If they requested a non-abbreviated Scientific format,
+        //  and there's an E## (but not E-##), add the missing '+' for E+##
+        String fslc = formatString.toLowerCase(Locale.ROOT);
+        if ((fslc.contains("general") || fslc.contains("e+0"))
+                && result.contains("E") && !result.contains("E-")) {
             result = result.replaceFirst("E", "E+");
         }
         return result;

Modified: 
poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java?rev=1876396&r1=1876395&r2=1876396&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
Sat Apr 11 15:36:27 2020
@@ -974,23 +974,47 @@ public class TestDataFormatter {
      * A numeric format string like 0E+0 should be E+
      */
     @Test
-    @Ignore("Bug #64319 is currently failing")
     public void testWithEinFormat() throws Exception {
         DataFormatter formatter = new DataFormatter();
+        
+        // Format string literals with an E in them shouldn't be
+        //  treated as a Scientific format, so shouldn't become E+
         assertEquals("TRUE", formatter.formatRawCellContents(1.0, 170, 
-                       "\"TRUE\";\"TRUE\";\"FALSE\""));
-        assertEquals("TRUE", formatter.formatRawCellContents(0.0, 170, 
-                       "\"TRUE\";\"TRUE\";\"FALSE\""));
+                       "\"TRUE\";\"FALSE\";\"ZERO\""));
+        assertEquals("ZERO", formatter.formatRawCellContents(0.0, 170, 
+                       "\"TRUE\";\"FALSE\";\"ZERO\""));
         assertEquals("FALSE", formatter.formatRawCellContents(-1.0, 170, 
-                       "\"TRUE\";\"TRUE\";\"FALSE\""));
+                       "\"TRUE\";\"FALSE\";\"ZERO\""));
+
+        // Explicit Scientific format does need E+
         assertEquals("1E+05", formatter.formatRawCellContents(1e05, 170, 
                        "0E+00"));
         assertEquals("1E+10", formatter.formatRawCellContents(1e10, 170, 
                        "0E+00"));
+        assertEquals("1E-10", formatter.formatRawCellContents(1e-10, 170, 
+                      "0E+00"));
+        
+        // Large numbers with "General" need E+
+        assertEquals("100000", formatter.formatRawCellContents(1e05, -1, 
"General"));
+        assertEquals("1E+12", formatter.formatRawCellContents(1e12, -1, 
"General"));
+
+        // Less common Scientific-like formats which don't ask for
+        //  the + on >1 exponentials don't need it adding
+        // (Java will put the -ve ones in for E-## automatically)
         assertEquals("1E5", formatter.formatRawCellContents(1e05, 170, 
                        "0E0"));
         assertEquals("1E10", formatter.formatRawCellContents(1e10, 170, 
                        "0E0"));
+        assertEquals("1E-10", formatter.formatRawCellContents(1e-10, 170, 
+                       "0E0"));
+
+        assertEquals("1E5", formatter.formatRawCellContents(1e05, 170, 
+                       "0E-0"));
+        assertEquals("1E10", formatter.formatRawCellContents(1e10, 170, 
+                       "0E-0"));
+        assertEquals("1E-10", formatter.formatRawCellContents(1e-10, 170, 
+                       "0E-0"));
+        
     }
 
     private void doFormatTestSequential(DataFormatter formatter) {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to