diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 94740e9d2..f8b09970e 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -312,14 +312,13 @@ public class DataFormatter implements Observer { String formatStr = formatStrIn; - // Excel supports 3+ part conditional data formats, eg positive/negative/zero, + // Excel supports 2+ part conditional data formats, eg positive/negative/zero, // or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds // of different formats for different ranges, just +ve/-ve, we need to // handle these ourselves in a special way. - // For now, if we detect 3+ parts, we call out to CellFormat to handle it + // For now, if we detect 2+ parts, we call out to CellFormat to handle it // TODO Going forward, we should really merge the logic between the two classes - if (formatStr.contains(";") && - formatStr.indexOf(';') != formatStr.lastIndexOf(';')) { + if (formatStr.contains(";") ) { try { // Ask CellFormat to get a formatter for it CellFormat cfmt = CellFormat.getInstance(formatStr); diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index 4b196316e..a703b80f2 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -198,6 +198,15 @@ public class TestDataFormatter { ); } } + + @Test + public void testConditionalRanges() { + DataFormatter dfUS = new DataFormatter(Locale.US); + + String format = "[>=10]#,##0;[<10]0.0"; + assertEquals("Wrong format for " + format, "17,876", dfUS.formatRawCellContents(17876.000, -1, format)); + assertEquals("Wrong format for " + format, "9.7", dfUS.formatRawCellContents(9.71, -1, format)); + } /** * Test how we handle negative and zeros. @@ -309,7 +318,8 @@ public class TestDataFormatter { //assertEquals("123", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0")); //Bug54868 patch has a hit on the first string before the ";" - assertEquals("-123 1/3", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0")); + assertEquals("123", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0")); + assertEquals("123 1/3", dfUS.formatRawCellContents(123.321, -1, "0 ?/?;0")); //Bug53150 formatting a whole number with fractions should just give the number assertEquals("1", dfUS.formatRawCellContents(1.0, -1, "# #/#"));