From b9d1e9c27f12676e17464e3d52bf4734b93fbd52 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 25 Nov 2021 05:07:01 +0000 Subject: [PATCH] [bug-63211] fix issue with escaped % in custom number format git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895312 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/usermodel/DataFormatter.java | 3 ++- .../apache/poi/ss/usermodel/TestDataFormatter.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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 a448b905fa..b8bd933a9a 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 @@ -313,7 +313,8 @@ public class DataFormatter { // int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2; // String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0]; - String formatStr = formatStrIn; + // this replace is done to fix https://bz.apache.org/bugzilla/show_bug.cgi?id=63211 + String formatStr = formatStrIn.replace("\\%", "\'%\'"); // Excel supports 2+ part conditional data formats, eg positive/negative/zero, // or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds 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 a3bb0684ad..9fbae9be49 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 @@ -1020,6 +1020,18 @@ class TestDataFormatter { } + @Test + void bug63211() { + DataFormatter formatter = new DataFormatter(); + // https://bz.apache.org/bugzilla/show_bug.cgi?id=63211 + // this format is an escaped % so is not the built-in percent which treats 0.125 as 12.5% + // this escaped format just appends a % to the raw decimal - so 12.5 becomes 12.5% + assertEquals("12.5%", + formatter.formatRawCellContents(12.5, -1, "0.0\\%;\\-0.0\\%")); + assertEquals("-12.5%", + formatter.formatRawCellContents(-12.5, -1, "0.0\\%;\\-0.0\\%")); + } + private void doFormatTestSequential(DataFormatter formatter) { for (int i = 0; i < 1_000; i++) { assertTrue(doFormatTest(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", i));