]> source.dussan.org Git - poi.git/commitdiff
Fix bug #61007
authorGreg Woolsey <gwoolsey@apache.org>
Wed, 19 Apr 2017 18:10:52 +0000 (18:10 +0000)
committerGreg Woolsey <gwoolsey@apache.org>
Wed, 19 Apr 2017 18:10:52 +0000 (18:10 +0000)
use CellFormat for all format strings containing multiple parts (";" delimited) and update unit test to expect the same values as Excel.  Also added tests for the failing formats.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1791949 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

index 94740e9d2b97b379ae7f8b324fa76344438ae282..f8b09970eef3756e55418f8e29d2c9ca490641d1 100644 (file)
@@ -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);
index 4b196316e76541687596d944cd19fad2dc387425..a703b80f29e13e172cd9955c7251b87edb231fea 100644 (file)
@@ -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, "# #/#"));