Skip to content

Commit

Permalink
Bug 65190: Handle decimal format '0#' the same way as Excel
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923056 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
centic9 committed Jan 11, 2025
1 parent 534d24d commit a4097e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("#").
Expand Down Expand Up @@ -850,6 +855,11 @@ private Format createNumberFormat(String formatStr, double cellValue) {
}
}

// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,27 @@ public void testFormatCellValue() throws IOException {
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#"));
}
}

0 comments on commit a4097e0

Please sign in to comment.