diff options
-rw-r--r-- | src/java/org/apache/poi/ss/usermodel/DataFormatter.java | 10 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java | 34 |
2 files changed, 36 insertions, 8 deletions
diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 12b3412bdd..b2312841c7 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -686,7 +686,7 @@ public class DataFormatter implements Observer { 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 Observer { 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; diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index 2949caed96..77f1ca3d3b 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -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) { |