This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new a45e77ce79 DataFormatter: scale each section by its own percent signs
(#1484)
a45e77ce79 is described below
commit a45e77ce7986fd7172eb0f7759de587a9a13ceb5
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Oct 26 17:18:04 2026 +0000
DataFormatter: scale each section by its own percent signs (#1484)
DecimalFormat applies the positive section's multiplier to negative
values, so "0%%;-0%" showed -0.25 as "-2500%" and "0%;-0" showed -5 as
"-500". Negative values now use a copy of the DecimalFormat with the
negative section's multiplier, as CellFormat does.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/ss/usermodel/DataFormatter.java | 37 ++++++++++++++++------
.../apache/poi/ss/usermodel/TestDataFormatter.java | 17 ++++++++++
2 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
index 9344bf640b..9328e6670f 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
@@ -1268,6 +1268,8 @@ public class DataFormatter {
private final BigDecimal divider;
private static final BigDecimal ONE_THOUSAND =
BigDecimal.valueOf(1000);
private final DecimalFormat df;
+ /** formats negative values: the same as df, apart from the multiplier
when the sections have different percent signs */
+ private final DecimalFormat negativeDf;
/** the integer and fraction digit placeholders of the positive
section, e.g. "??0" and "0?" */
private final String integerPlaceholders;
private final String fractionPlaceholders;
@@ -1303,25 +1305,32 @@ public class DataFormatter {
if (padsWithSpaces) {
pattern = pattern.replace('?', '#');
}
- // each '%' multiplies by 100, but DecimalFormat only allows one
in each section;
- // the value is scaled by the percent signs of the positive
section only
+ // each '%' multiplies by 100, but DecimalFormat only allows one
in each section,
+ // and it scales negative values by the percent signs of the
positive section
int percents = countUnquotedInSection(pattern, '%', 0);
- boolean quotePercents = false;
- for (int section = 0; section < 2; section++) {
- quotePercents |= countUnquotedInSection(pattern, '%', section)
> 1;
+ int negativePercents = countUnquotedInSection(pattern, '%', 1);
+ if (negativePercents < 0) {
+ negativePercents = percents;
}
+ boolean quotePercents = percents > 1 || negativePercents > 1 ||
percents != negativePercents;
if (quotePercents) {
pattern = quoteUnquoted(pattern, '%');
}
df = new DecimalFormat(trimTrailingCommas(pattern), symbols);
if (quotePercents) {
- df.setMultiplier((int) Math.pow(100, Math.min(percents, 4)));
+ df.setMultiplier(percentMultiplier(percents));
}
setExcelStyleRoundingMode(df);
// Excel shows the decimal point even when there are no digits
after it, e.g. "#.#" shows 1 as "1."
if (hasDecimalPoint(pattern)) {
df.setDecimalSeparatorAlwaysShown(true);
}
+ if (negativePercents == percents) {
+ negativeDf = df;
+ } else {
+ negativeDf = (DecimalFormat) df.clone();
+ negativeDf.setMultiplier(percentMultiplier(negativePercents));
+ }
Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
if (endsWithCommasMatcher.find()) {
String commas = (endsWithCommasMatcher.group(1));
@@ -1335,7 +1344,14 @@ public class DataFormatter {
}
}
- /** counts the unquoted {@code ch} in a section of the pattern: 0 is
the positive section, 1 the negative one */
+ private static int percentMultiplier(int percents) {
+ return (int) Math.pow(100, Math.min(percents, 4));
+ }
+
+ /**
+ * counts the unquoted {@code ch} in a section of the pattern: 0 is
the positive section, 1 the negative one;
+ * -1 when the pattern does not have the section
+ */
private static int countUnquotedInSection(String pattern, char ch, int
section) {
int count = 0;
int currentSection = 0;
@@ -1350,7 +1366,7 @@ public class DataFormatter {
count++;
}
}
- return count;
+ return currentSection < section ? -1 : count;
}
/** quotes each run of unquoted {@code ch}, so that DecimalFormat
shows them as text */
@@ -1415,10 +1431,11 @@ public class DataFormatter {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
obj = scaleInput(obj);
+ DecimalFormat format = obj instanceof Number && ((Number)
obj).doubleValue() < 0 ? negativeDf : df;
if (!padsWithSpaces) {
- return df.format(obj, toAppendTo, pos);
+ return format.format(obj, toAppendTo, pos);
}
- return toAppendTo.append(padWithSpaces(df.format(obj)));
+ return toAppendTo.append(padWithSpaces(format.format(obj)));
}
/**
diff --git
a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
index 9feac7e642..fd00a4d84d 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
@@ -1441,6 +1441,23 @@ class TestDataFormatter {
assertEquals("-2023.45%%", formatter.formatRawCellContents(-0.202345,
-1, "0.00%%;-0.00%%"));
}
+ /**
+ * Each section is scaled by its own percent signs
+ */
+ @Test
+ void percentDiffersBetweenSections() {
+ DataFormatter formatter = new DataFormatter();
+ assertEquals("2500%%", formatter.formatRawCellContents(0.25, -1,
"0%%;-0%"));
+ assertEquals("-25%", formatter.formatRawCellContents(-0.25, -1,
"0%%;-0%"));
+ assertEquals("25%", formatter.formatRawCellContents(0.25, -1,
"0%;-0%%"));
+ assertEquals("-2500%%", formatter.formatRawCellContents(-0.25, -1,
"0%;-0%%"));
+ assertEquals("25%", formatter.formatRawCellContents(0.25, -1,
"0%;-0"));
+ assertEquals("-5", formatter.formatRawCellContents(-5, -1, "0%;-0"));
+ assertEquals("5", formatter.formatRawCellContents(5, -1, "0;-0%"));
+ assertEquals("-500%", formatter.formatRawCellContents(-5, -1,
"0;-0%"));
+ assertEquals("(5.0)", formatter.formatRawCellContents(-5, -1,
"0.0%;(0.0)"));
+ }
+
@Test
void testAdjustTo4DigitYearsIfConfigured() {
DataFormatter dataFormatter = new DataFormatter();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]