#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

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876396 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2020-04-11 15:36:27 +00:00
parent 0ea6b0800d
commit 2b2631225d
2 changed files with 36 additions and 8 deletions

View File

@ -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;

View File

@ -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) {