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 5de26e8469 DataFormatter: percent format with a negative section 
multiplied by 100 twice (#1483)
5de26e8469 is described below

commit 5de26e846918e4fbbeadce96ecdef0c92548a8b9
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Oct 26 15:54:52 2026 +0000

    DataFormatter: percent format with a negative section multiplied by 100 
twice (#1483)
    
    DataFormatter: only the positive section's percent signs scale the value
    
    A percent format with a negative section, e.g. "0.00%;[Red]-0.00%", was
    multiplied by 100 twice since #1404, because the percent signs of both
    sections were counted. Count them per section.
    
    Fixes #1482
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/poi/ss/usermodel/DataFormatter.java  | 21 +++++++++++++++------
 .../apache/poi/ss/usermodel/TestDataFormatter.java  | 14 ++++++++++++++
 2 files changed, 29 insertions(+), 6 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 09678a5fa1..46315ee959 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
@@ -1295,13 +1295,18 @@ public class DataFormatter {
             if (padsWithSpaces) {
                 pattern = pattern.replace('?', '#');
             }
-            // each '%' multiplies by 100, but DecimalFormat only allows one
-            int percents = countUnquoted(pattern, '%');
-            if (percents > 1) {
+            // 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
+            int percents = countUnquotedInSection(pattern, '%', 0);
+            boolean quotePercents = false;
+            for (int section = 0; section < 2; section++) {
+                quotePercents |= countUnquotedInSection(pattern, '%', section) 
> 1;
+            }
+            if (quotePercents) {
                 pattern = quoteUnquoted(pattern, '%');
             }
             df = new DecimalFormat(trimTrailingCommas(pattern), symbols);
-            if (percents > 1) {
+            if (quotePercents) {
                 df.setMultiplier((int) Math.pow(100, Math.min(percents, 4)));
             }
             setExcelStyleRoundingMode(df);
@@ -1322,14 +1327,18 @@ public class DataFormatter {
             }
         }
 
-        private static int countUnquoted(String pattern, char ch) {
+        /** counts the unquoted {@code ch} in a section of the pattern: 0 is 
the positive section, 1 the negative one */
+        private static int countUnquotedInSection(String pattern, char ch, int 
section) {
             int count = 0;
+            int currentSection = 0;
             boolean quoted = false;
             for (int i = 0; i < pattern.length(); i++) {
                 char c = pattern.charAt(i);
                 if (c == '\'') {
                     quoted = !quoted;
-                } else if (!quoted && c == ch) {
+                } else if (!quoted && c == ';') {
+                    currentSection++;
+                } else if (!quoted && c == ch && currentSection == section) {
                     count++;
                 }
             }
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 d8cac2692e..115b3afca2 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
@@ -1427,6 +1427,20 @@ class TestDataFormatter {
                 formatter.formatRawCellContents(-12.5, -1, 
"0.0\\%;\\-0.0\\%"));
     }
 
+    /**
+     * See https://github.com/apache/poi/issues/1482
+     */
+    @Test
+    void percentWithNegativeSection() {
+        DataFormatter formatter = new DataFormatter();
+        assertEquals("20.23%", formatter.formatRawCellContents(0.202345, -1, 
"0.00%;[Red]-0.00%"));
+        assertEquals("-20.23%", formatter.formatRawCellContents(-0.202345, -1, 
"0.00%;[Red]-0.00%"));
+        assertEquals("1,020.23%", formatter.formatRawCellContents(10.202345, 
-1, "#,##0.00%;[Red]-#,##0.00%"));
+        assertEquals("-1,020.23%", formatter.formatRawCellContents(-10.202345, 
-1, "#,##0.00%;[Red]-#,##0.00%"));
+        assertEquals("2023.45%%", formatter.formatRawCellContents(0.202345, 
-1, "0.00%%;-0.00%%"));
+        assertEquals("-2023.45%%", formatter.formatRawCellContents(-0.202345, 
-1, "0.00%%;-0.00%%"));
+    }
+
     @Test
     void testAdjustTo4DigitYearsIfConfigured() {
         DataFormatter dataFormatter = new DataFormatter();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to