issue when formatting number when divisor is needed (#895)

* reproduce issue 69812

* workaround for issue
This commit is contained in:
PJ Fanning 2025-09-09 22:27:48 +01:00 committed by GitHub
parent 559485a0fe
commit d0e6830e44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 6 deletions

View File

@ -3938,6 +3938,20 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
}
}
@Test
void testBug69812() throws Exception {
try (XSSFWorkbook wb = openSampleWorkbook("bug69812.xlsx")) {
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
XSSFCell cellA1 = row.getCell(0);
DataFormatter dataFormatter = new DataFormatter();
String cellValue = dataFormatter.formatCellValue(cellA1);
// https://bz.apache.org/bugzilla/show_bug.cgi?id=69812: user says this should be "25,386"
assertEquals("25,396", cellValue);
assertEquals("#,##0,,", cellA1.getCellStyle().getDataFormatString());
}
}
private static void readByCommonsCompress(File temp_excel_poi) throws IOException {
/* read by commons-compress*/
try (ZipFile zipFile = ZipFile.builder().setFile(temp_excel_poi).get()) {

View File

@ -808,6 +808,10 @@ public class DataFormatter {
}
}
boolean requiresScaling() {
return divider != null;
}
private Object scaleInput(Object obj) {
if (divider != null) {
if (obj instanceof BigDecimal) {
@ -965,12 +969,22 @@ public class DataFormatter {
if (numberFormat == null) {
return Double.toString(d);
}
String formatted;
try {
//see https://github.com/apache/poi/pull/321 -- but this sometimes fails, thus the catch and retry
formatted = numberFormat.format(BigDecimal.valueOf(d));
} catch (NumberFormatException nfe) {
formatted = numberFormat.format(d);
String formatted = null;
if (numberFormat instanceof InternalDecimalFormatWithScale) {
InternalDecimalFormatWithScale idfws = (InternalDecimalFormatWithScale) numberFormat;
if (idfws.requiresScaling()) {
// hack for https://bz.apache.org/bugzilla/show_bug.cgi?id=69812
// the https://github.com/apache/poi/pull/321 hack causes problems here
formatted = idfws.format(d);
}
}
if (formatted == null) {
try {
//see https://github.com/apache/poi/pull/321 -- but this sometimes fails, thus the catch and retry
formatted = numberFormat.format(BigDecimal.valueOf(d));
} catch (NumberFormatException nfe) {
formatted = numberFormat.format(d);
}
}
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
}

Binary file not shown.