poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1712605 - in /poi/trunk/src: java/org/apache/poi/ss/usermodel/DataFormatter.java testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java
Date Wed, 04 Nov 2015 18:23:17 GMT
Author: nick
Date: Wed Nov  4 18:23:17 2015
New Revision: 1712605

URL: http://svn.apache.org/viewvc?rev=1712605&view=rev
Log:
Bug #55265 - DataFormatter correct support for alternate number grouping characters, eg 1234
+ #'##0 = 1'234 not 1,234

Modified:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.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=1712605&r1=1712604&r2=1712605&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 Wed Nov  4 18:23:17
2015
@@ -158,6 +158,12 @@ public class DataFormatter implements Ob
     private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");
 
     /**
+     * A regex to detect if an alternate grouping character is used
+     *  in a numeric format 
+     */
+    private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
+    
+    /**
       * Cells formatted with a date or time format and which contain invalid date or time
values
      *  show 255 pound signs ("#").
       */
@@ -658,10 +664,24 @@ public class DataFormatter implements Ob
     }
 
     private Format createNumberFormat(String formatStr, double cellValue) {
-        final String format = cleanFormatForNumber(formatStr);
+        String format = cleanFormatForNumber(formatStr);
+        DecimalFormatSymbols symbols = decimalSymbols;
+        
+        // Do we need to change the grouping character?
+        // eg for a format like #'##0 which wants 12'345 not 12,345
+        Matcher agm = alternateGrouping.matcher(format);
+        if (agm.find()) {
+            symbols = DecimalFormatSymbols.getInstance(locale);
+            
+            char grouping = agm.group(2).charAt(0);
+            symbols.setGroupingSeparator(grouping);
+            String oldPart = agm.group(1);
+            String newPart = oldPart.replace(grouping, ',');
+            format = format.replace(oldPart, newPart);
+        }
         
         try {
-            DecimalFormat df = new DecimalFormat(format, decimalSymbols);
+            DecimalFormat df = new DecimalFormat(format, symbols);
             setExcelStyleRoundingMode(df);
             return df;
         } catch(IllegalArgumentException iae) {

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java?rev=1712605&r1=1712604&r2=1712605&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java Wed Nov  4
18:23:17 2015
@@ -165,4 +165,40 @@ public abstract class BaseTestDataFormat
         // TODO Fix this to not have an extra 0 at the end
         //assertEquals(pound+"   -  ", formatter.formatCellValue(zero)); 
     }
+    
+    /**
+     * Using a single quote (') instead of a comma (,) as
+     *  a number separator, eg 1000 -> 1'000 
+     */
+    public final void test55265() {
+        Workbook wb = _testDataProvider.createWorkbook();
+        DataFormatter formatter = new DataFormatter();
+        DataFormat fmt = wb.createDataFormat();
+        Sheet sheet = wb.createSheet();
+        Row r = sheet.createRow(0);
+        
+        CellStyle cs = wb.createCellStyle();
+        cs.setDataFormat(fmt.getFormat("#'##0"));
+        
+        Cell zero = r.createCell(0);
+        zero.setCellValue(0);
+        zero.setCellStyle(cs);
+        
+        Cell sml = r.createCell(1);
+        sml.setCellValue(12);
+        sml.setCellStyle(cs);
+        
+        Cell med = r.createCell(2);
+        med.setCellValue(1234);
+        med.setCellStyle(cs);
+        
+        Cell lge = r.createCell(3);
+        lge.setCellValue(12345678);
+        lge.setCellStyle(cs);
+        
+        assertEquals("0", formatter.formatCellValue(zero)); 
+        assertEquals("12", formatter.formatCellValue(sml)); 
+        assertEquals("1'234", formatter.formatCellValue(med)); 
+        assertEquals("12'345'678", formatter.formatCellValue(lge)); 
+    }
 }



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


Mime
View raw message