Bug 65190: Handle decimal format '0#' the same way as Excel

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923056 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2025-01-11 09:23:55 +00:00
parent 534d24dc74
commit a4097e05aa
2 changed files with 33 additions and 0 deletions

View File

@ -162,6 +162,11 @@ public class DataFormatter {
*/
private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
/**
* For handling '0#' properly
*/
private static final Pattern decimalFormatFix = Pattern.compile("0+#");
/**
* Cells formatted with a date or time format and which contain invalid date or time values
* show 255 pound signs ("#").
@ -850,6 +855,11 @@ public class DataFormatter {
}
}
// Excel ignores leading zeros, but Java fails with an exception below
if (decimalFormatFix.matcher(format).matches()) {
format = "#";
}
try {
return new InternalDecimalFormatWithScale(format, symbols);
} catch(IllegalArgumentException iae) {

View File

@ -1207,4 +1207,27 @@ class TestDataFormatter {
assertEquals("25571.751069247686", df.formatCellValue(cell));*/
}
}
@Test
void testBug65190() {
DataFormatter formatter = new DataFormatter(Locale.ENGLISH);
assertEquals("12334567890",
formatter.formatRawCellContents(12334567890.0, 0, "0"));
assertEquals("12334567890",
formatter.formatRawCellContents(12334567890.0, 0, "#"));
assertEquals("12334567890",
formatter.formatRawCellContents(12334567890.0, 0, "#0"));
assertEquals("12334567890",
formatter.formatRawCellContents(12334567890.0, 0, "0#"));
assertEquals("12334567890123",
formatter.formatRawCellContents(12334567890123.0, 0, "0"));
assertEquals("12334567890123",
formatter.formatRawCellContents(12334567890123.0, 0, "#"));
assertEquals("12334567890123",
formatter.formatRawCellContents(12334567890123.0, 0, "#0"));
assertEquals("12334567890123",
formatter.formatRawCellContents(12334567890123.0, 0, "0#"));
}
}