← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11397: Made numeric regex now support scientific notation, java double style. Makes sure very large numb...

 

------------------------------------------------------------
revno: 11397
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-07-15 20:17:40 +0200
message:
  Made numeric regex now support scientific notation, java double style. Makes sure very large numbers are rendered in general notation in pivot table excel download.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-07-15 16:39:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-07-15 18:17:40 +0000
@@ -46,8 +46,8 @@
     
     private static final double TOLERANCE = 0.01; 
     
-    public static final String NUMERIC_REGEXP = "^(-?0|-?[1-9]\\d*)(\\.\\d+)?$";
-    public static final String NUMERIC_LENIENT_REGEXP = "^(-?[0-9]+)(\\.[0-9]+)?$";
+    public static final String NUMERIC_REGEXP = "^(-?0|-?[1-9]\\d*)(\\.\\d+)?(E\\d+)?$";
+    public static final String NUMERIC_LENIENT_REGEXP = "^(-?[0-9]+)(\\.[0-9]+)?(E\\d+)?$";
     
     private static final Pattern NUMERIC_PATTERN = Pattern.compile( NUMERIC_REGEXP );
     private static final Pattern NUMERIC_LENIENT_PATTERN = Pattern.compile( NUMERIC_LENIENT_REGEXP );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-07-15 16:39:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-07-15 18:17:40 +0000
@@ -100,6 +100,8 @@
         assertTrue( MathUtils.isNumeric( "-1234" ) );
         assertTrue( MathUtils.isNumeric( "-12.34" ) );
         assertTrue( MathUtils.isNumeric( "-0.34" ) );
+        assertTrue( MathUtils.isNumeric( "6.34E11" ) );
+        assertTrue( MathUtils.isNumeric( "3.342E7" ) );
 
         assertFalse( MathUtils.isNumeric( "Hey" ) );
         assertFalse( MathUtils.isNumeric( "45 Perinatal Condition" ) );
@@ -122,6 +124,9 @@
         assertFalse( MathUtils.isNumeric( "001" ) );
         assertFalse( MathUtils.isNumeric( "00.23" ) );
         assertFalse( MathUtils.isNumeric( "01.23" ) );
+        assertFalse( MathUtils.isNumeric( "4.23E" ) );
+        assertFalse( MathUtils.isNumeric( "4.23Ef" ) );
+        assertFalse( MathUtils.isNumeric( "E5" ) );
         assertFalse( MathUtils.isNumeric( null ) );
     }
 
@@ -139,6 +144,8 @@
         assertTrue( MathUtils.isNumericLenient( "-1234" ) );
         assertTrue( MathUtils.isNumericLenient( "-12.34" ) );
         assertTrue( MathUtils.isNumericLenient( "-0.34" ) );
+        assertTrue( MathUtils.isNumericLenient( "6.34E11" ) );
+        assertTrue( MathUtils.isNumericLenient( "3.342E7" ) );
 
         assertFalse( MathUtils.isNumericLenient( "Hey" ) );
         assertFalse( MathUtils.isNumericLenient( "45 Perinatal Condition" ) );
@@ -158,6 +165,9 @@
         assertFalse( MathUtils.isNumericLenient( "0,1" ) );
         assertFalse( MathUtils.isNumericLenient( "0," ) );
         assertFalse( MathUtils.isNumericLenient( "0." ) );
+        assertFalse( MathUtils.isNumericLenient( "4.23E" ) );
+        assertFalse( MathUtils.isNumericLenient( "4.23Ef" ) );
+        assertFalse( MathUtils.isNumericLenient( "E5" ) );
         assertFalse( MathUtils.isNumericLenient( null ) );
     }