← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11424: Analytics, rounding, now using 2 decimals if value is between 1 and -1, otherwise 1 decimal. Not ...

 

------------------------------------------------------------
revno: 11424
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-07-17 21:40:31 +0200
message:
  Analytics, rounding, now using 2 decimals if value is between 1 and -1, otherwise 1 decimal. Not ideal but better than current.
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  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-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-07-04 15:41:57 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-07-17 19:40:31 +0000
@@ -254,7 +254,7 @@
                                                         
                             grid.addRow();
                             grid.addValues( DimensionItem.getItemIdentifiers( row ) );
-                            grid.addValue( MathUtils.getRounded( value, 1 ) );
+                            grid.addValue( MathUtils.getRounded( value ) );
                         }
                     }
                 }
@@ -277,7 +277,7 @@
             {
                 grid.addRow();
                 grid.addValues( entry.getKey().split( DIMENSION_SEP ) );
-                grid.addValue( entry.getValue() );
+                grid.addValue( MathUtils.getRounded( entry.getValue() ) );
             }
         }
 
@@ -343,7 +343,7 @@
                     
                     grid.addRow();
                     grid.addValues( dataRow.toArray() );
-                    grid.addValue( MathUtils.getRounded( value, 1 ) );
+                    grid.addValue( MathUtils.getRounded( value ) );
                 }
             }
         }
@@ -360,7 +360,7 @@
             {
                 grid.addRow();
                 grid.addValues( entry.getKey().split( DIMENSION_SEP ) );
-                grid.addValue( entry.getValue() );
+                grid.addValue( MathUtils.getRounded( entry.getValue() ) );
             }
         }
 

=== 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 18:17:40 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-07-17 19:40:31 +0000
@@ -147,10 +147,33 @@
         
         return Math.round( value * factor ) / factor;
     }
+    
+    /**
+     * Return a rounded off number.
+     * 
+     * <ul>
+     * <li>If value is exclusively between 1 and -1 it will have 2 decimals.</li>
+     * <li>If value if greater or equal to 1 the value will have 1 decimal.</li>
+     * </ul>
+     * 
+     * @param value the value to round off.
+     * @return a rounded off number.
+     */
+    public static double getRounded( double value )
+    {
+        if ( value < 1d && value > -1d )
+        {
+            return getRounded( value, 2 );
+        }
+        else
+        {
+            return getRounded( value, 1 );
+        }
+    }
 
     /**
-     * Returns a string representation of number rounded to given number
-     * of significant figures
+     * Returns a string representation of number rounded to given number of
+     * significant figures
      * 
      * @param value
      * @param significantFigures
@@ -158,8 +181,8 @@
      */
     public static String roundToString( double value, int significantFigures )
     {
-        MathContext mc = new MathContext(significantFigures);
-        BigDecimal num = new BigDecimal(value);
+        MathContext mc = new MathContext( significantFigures );
+        BigDecimal num = new BigDecimal( value );
         return num.round( mc ).toPlainString();
     }
 

=== 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 18:17:40 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-07-17 19:40:31 +0000
@@ -241,4 +241,16 @@
     {
         assertEquals( 7.5, MathUtils.getAverage( Arrays.asList( 5.0, 5.0, 10.0, 10.0 ) ), 0.01 );
     }
+    
+    @Test
+    public void testGetRounded()
+    {
+        assertEquals( 10, MathUtils.getRounded( 10.00 ), 0.01 );
+        assertEquals( 10, MathUtils.getRounded( 10 ), 0.01 );
+        assertEquals( 0.53, MathUtils.getRounded( 0.5281 ), 0.01 );
+        assertEquals( 0.5, MathUtils.getRounded( 0.5 ), 0.01 );
+        assertEquals( 0, MathUtils.getRounded( 0 ), 0.01 );
+        assertEquals( -0.43, MathUtils.getRounded( -0.43123 ), 0.01 );
+        assertEquals( -10, MathUtils.getRounded( -10.00 ), 0.01 );        
+    }
 }