Author: fanningpj
Date: Thu Jul  6 07:56:57 2017
New Revision: 1800975

URL: http://svn.apache.org/viewvc?rev=1800975&view=rev
Log:
[github-25] support excel number trailing comma format - Thabks to Luca Martini 
for proving the patch

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

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java?rev=1800975&r1=1800974&r2=1800975&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java Thu Jul  
6 07:56:57 2017
@@ -687,6 +687,57 @@ public class DataFormatter implements Ob
         return sb.toString();
     }
 
+    private static class InternalDecimalFormatWithScale extends Format {
+
+        private static final Pattern endsWithCommas = Pattern.compile("(,+)$");
+        private BigDecimal divider;
+        private static final BigDecimal ONE_THOUSAND = new BigDecimal(1000);
+        private final DecimalFormat df;
+        private static final String trimTrailingCommas(String s) {
+            return s.replaceAll(",+$", "");
+        }
+
+        public InternalDecimalFormatWithScale(String pattern, 
DecimalFormatSymbols symbols) {
+            df = new DecimalFormat(trimTrailingCommas(pattern), symbols);
+            setExcelStyleRoundingMode(df);
+            Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
+            if (endsWithCommasMatcher.find()) {
+                String commas = (endsWithCommasMatcher.group(1));
+                BigDecimal temp = BigDecimal.ONE;
+                for (int i = 0; i < commas.length(); ++i) {
+                    temp = temp.multiply(ONE_THOUSAND);
+                }
+                divider = temp;
+            } else {
+                divider = null;
+            }
+        }
+
+        private Object scaleInput(Object obj) {
+            if (divider != null) {
+                if (obj instanceof BigDecimal) {
+                    obj = ((BigDecimal) obj).divide(divider, 
RoundingMode.HALF_UP);
+                } else if (obj instanceof Double) {
+                    obj = (Double) obj / divider.doubleValue();
+                } else {
+                    throw new UnsupportedOperationException();
+                }
+            }
+            return obj;
+        }
+
+        @Override
+        public StringBuffer format(Object obj, StringBuffer toAppendTo, 
FieldPosition pos) {
+            obj = scaleInput(obj);
+            return df.format(obj, toAppendTo, pos);
+        }
+
+        @Override
+        public Object parseObject(String source, ParsePosition pos) {
+            throw new UnsupportedOperationException();
+        }
+    }
+
     private Format createNumberFormat(String formatStr, double cellValue) {
         String format = cleanFormatForNumber(formatStr);
         DecimalFormatSymbols symbols = decimalSymbols;
@@ -710,9 +761,7 @@ public class DataFormatter implements Ob
         }
         
         try {
-            DecimalFormat df = new DecimalFormat(format, symbols);
-            setExcelStyleRoundingMode(df);
-            return df;
+            return new InternalDecimalFormatWithScale(format, symbols);
         } catch(IllegalArgumentException iae) {
             logger.log(POILogger.DEBUG, "Formatting failed for format " + 
formatStr + ", falling back", iae);
             // the pattern could not be parsed correctly,
@@ -1247,4 +1296,4 @@ public class DataFormatter implements Ob
             return null; // Not supported
         }
     }
-}
+}
\ No newline at end of file

Modified: 
poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java?rev=1800975&r1=1800974&r2=1800975&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java 
Thu Jul  6 07:56:57 2017
@@ -200,7 +200,7 @@ public class TestDataFormatter {
             );
         }
     }
-    
+
     @Test
     public void testConditionalRanges() {
         DataFormatter dfUS = new DataFormatter(Locale.US);
@@ -820,7 +820,37 @@ public class TestDataFormatter {
 
         wb.close();
     }
-    
+
+    @Test
+    public void testFormatWithTrailingDotsUS() {
+        DataFormatter dfUS = new DataFormatter(Locale.US);
+        assertEquals("1,000,000", dfUS.formatRawCellContents(1000000, -1, 
"#,##0"));
+        assertEquals("1,000", dfUS.formatRawCellContents(1000000, -1, 
"#,##0,"));
+        assertEquals("1", dfUS.formatRawCellContents(1000000, -1, "#,##0,,"));
+        assertEquals("1,000,000.0", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.0"));
+        assertEquals("1,000.0", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.0,"));
+        assertEquals("1.0", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.0,,"));
+        assertEquals("1,000,000.00", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.00"));
+        assertEquals("1,000.00", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.00,"));
+        assertEquals("1.00", dfUS.formatRawCellContents(1000000, -1, 
"#,##0.00,,"));
+        assertEquals("1,000,000", dfUS.formatRawCellContents(1e24, -1, 
"#,##0,,,,,,"));
+    }
+
+    @Test
+    public void testFormatWithTrailingDotsOtherLocale() throws Exception {
+        DataFormatter dfIT = new DataFormatter(Locale.ITALY);
+        assertEquals("1.000.000", dfIT.formatRawCellContents(1000000, -1, 
"#,##0"));
+        assertEquals("1.000", dfIT.formatRawCellContents(1000000, -1, 
"#,##0,"));
+        assertEquals("1", dfIT.formatRawCellContents(1000000, -1, "#,##0,,"));
+        assertEquals("1.000.000,0", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.0"));
+        assertEquals("1.000,0", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.0,"));
+        assertEquals("1,0", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.0,,"));
+        assertEquals("1.000.000,00", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.00"));
+        assertEquals("1.000,00", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.00,"));
+        assertEquals("1,00", dfIT.formatRawCellContents(1000000, -1, 
"#,##0.00,,"));
+        assertEquals("1.000.000", dfIT.formatRawCellContents(1e24, -1, 
"#,##0,,,,,,"));
+    }
+
     /**
      * bug 60031: DataFormatter parses months incorrectly when put at the end 
of date segment
      */
@@ -895,4 +925,4 @@ public class TestDataFormatter {
         assertEquals("4,33 " + euro, df.formatRawCellContents(4.33, 178, 
formatString));
         assertEquals("1.234,33 " + euro, df.formatRawCellContents(1234.33, 
178, formatString));
     }
-}
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org
For additional commands, e-mail: commits-h...@poi.apache.org

Reply via email to