← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2559: Keep full precision for annualization value in indicator object.

 

------------------------------------------------------------
revno: 2559
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: trunk
timestamp: Thu 2011-01-13 15:38:43 +0000
message:
  Keep full precision for annualization value in indicator object.
  Implemented rounding method to significant figures rather than decimal places.
  Round floating point values to 5 significant figures on csv output.
modified:
  dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/indicator/DefaultIndicatorDataMart.java
  dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/synchronous/ExportPivotViewService.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-datamart-default/src/main/java/org/hisp/dhis/datamart/indicator/DefaultIndicatorDataMart.java'
--- dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/indicator/DefaultIndicatorDataMart.java	2011-01-13 13:27:54 +0000
+++ dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/indicator/DefaultIndicatorDataMart.java	2011-01-13 15:38:43 +0000
@@ -187,7 +187,7 @@
                         indicatorValue.setOrganisationUnitId( unit.getId() );
                         indicatorValue.setLevel( level );
                         indicatorValue.setAnnualized( getAnnualizationString( indicator.getAnnualized() ) );
-                        indicatorValue.setFactor( getRounded( annualizedFactor, DECIMALS ) );
+                        indicatorValue.setFactor( annualizedFactor);
                         indicatorValue.setValue( getRounded( aggregatedValue, DECIMALS ) );
                         indicatorValue.setNumeratorValue( getRounded( numeratorValue, DECIMALS ) );
                         indicatorValue.setDenominatorValue( getRounded( denominatorValue, DECIMALS ) );

=== modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/synchronous/ExportPivotViewService.java'
--- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/synchronous/ExportPivotViewService.java	2011-01-13 11:20:59 +0000
+++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/synchronous/ExportPivotViewService.java	2011-01-13 15:38:43 +0000
@@ -47,6 +47,7 @@
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.system.util.MathUtils;
 
 /**
  * Exports pivot view synchronously (using calling thread)
@@ -62,6 +63,9 @@
     // service can export either aggregated datavalues or aggregated indicator values
     public enum RequestType { DATAVALUE, INDICATORVALUE };
 
+    // precision to use when formatting double values
+    public static int PRECISION = 5;
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -172,7 +176,7 @@
 
         AggregatedIndicatorValue aiv = Iterator.next();
 
-        writer.write("# period, orgunit, indicator, factor, numerator, denominator, annualized, value\n");
+        writer.write("# period, orgunit, indicator, factor, numerator, denominator\n");
         while (aiv != null)
         {
             // process adv ..
@@ -182,11 +186,9 @@
             writer.write( "'" + period + "',");
             writer.write( aiv.getOrganisationUnitId() + ",");
             writer.write( aiv.getIndicatorId() + ",");
-            writer.write( aiv.getFactor() + ",");
-            writer.write( aiv.getNumeratorValue() + ",");
-            writer.write( aiv.getDenominatorValue() + ",");
-            writer.write( "'" + aiv.getAnnualized() + "',");
-            writer.write( aiv.getValue() + "\n");
+            writer.write( MathUtils.roundToString( aiv.getFactor(), PRECISION) + ",");
+            writer.write( MathUtils.roundToString(aiv.getNumeratorValue(), PRECISION) + ",");
+            writer.write( MathUtils.roundToString(aiv.getDenominatorValue(), PRECISION) + "\n");
 
             aiv = Iterator.next();
         }

=== 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	2010-12-29 10:06:33 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2011-01-13 15:38:43 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.math.BigDecimal;
+import java.math.MathContext;
 import java.util.Random;
 import java.util.Scanner;
 
@@ -134,7 +136,22 @@
         
         return Math.round( value * factor ) / factor;
     }
-    
+
+    /**
+     * Returns a string representation of number rounded to given number
+     * of significant figures
+     * 
+     * @param value
+     * @param significantFigures
+     * @return
+     */
+    public static String roundToString(double value, int significantFigures)
+    {
+        MathContext mc = new MathContext(significantFigures);
+        BigDecimal num = new BigDecimal(value);
+        return num.round( mc ).toPlainString();
+    }
+
     /**
      * Returns the given number if larger or equal to minimun, otherwise minimum.
      * 

=== 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	2010-12-29 10:06:33 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2011-01-13 15:38:43 +0000
@@ -69,4 +69,16 @@
         
         assertEquals( 12.0, MathUtils.getMax( array ) );
     }
+
+    @Test
+    public void testRounding()
+    {
+        double[] numbers = { 34532.0, 23467000.0, 0.0034568 };
+        String [] rounded = {"34530", "23470000", "0.003457" };
+
+        for (int i=0; i < numbers.length; ++i)
+        {
+            assertEquals(rounded[i], MathUtils.roundToString( numbers[i], 4));
+        }
+    }
 }