Revision: 4878
          http://sourceforge.net/p/vexi/code/4878
Author:   mkpg2
Date:     2016-08-07 16:04:12 +0000 (Sun, 07 Aug 2016)
Log Message:
-----------
New method Number.roundSF(). Round to significant figures.
- takes 2 args, optional scale (decimal places) as well. Useful for rounding 
monetary values, where sub penny values may be desirable, but never want to 
round pennies away.

Modified Paths:
--------------
    branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSNumber.java
    branches/vexi3/org.vexi-library.value/meta/module.xml
    
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java
    
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java

Modified: 
branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSNumber.java
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSNumber.java  
2016-07-21 12:02:46 UTC (rev 4877)
+++ branches/vexi3/org.vexi-library.js/src/main/java/org/ibex/js/JSNumber.java  
2016-08-07 16:04:12 UTC (rev 4878)
@@ -108,6 +108,7 @@
         String keyStr = JSU.toString(key);
         if("format".equals(keyStr)) return typeConst2string(kind());
         else if("round".equals(keyStr)) return METHOD;
+        else if("roundSF".equals(keyStr)) return METHOD;
         else if("toDecimalString".equals(keyStr)) return METHOD;
         return super.get(key);
     }
@@ -116,6 +117,11 @@
         if("round".equals(keyStr)){
             int scale = JSU.getArg_int(args, 0, 0);
             return round(scale, RoundingMode.HALF_UP);
+        }else if("roundSF".equals(keyStr)){
+            int sigfigs = JSU.expectArg_int(args, 0);
+            JS scaleJS = JSU.getArg(args, 1);
+            Integer scale = scaleJS==null?null:JSU.toInt(scaleJS);
+            return JSU.N(toRational().roundSF(sigfigs, scale));
         }else if("toDecimalString".equals(keyStr)){
             if(this instanceof D){
                 double d = ((D)this).d;

Modified: branches/vexi3/org.vexi-library.value/meta/module.xml
===================================================================
--- branches/vexi3/org.vexi-library.value/meta/module.xml       2016-07-21 
12:02:46 UTC (rev 4877)
+++ branches/vexi3/org.vexi-library.value/meta/module.xml       2016-08-07 
16:04:12 UTC (rev 4878)
@@ -3,7 +3,7 @@
        <artifact name="java.zip" />
     
     <dependencies>
-               <system  name="java.jre"        tag="1.4"/>
+               <system  name="java.jre"        tag="1.6"/>
            <!-- Test dependencies -->
            <dependency source="local"   org="org.vexi"            
name="library.testing"           branch="trunk"             scope="test"/>
            <dependency source="ibiblio" org="junit"               name="junit" 
                                 tag="3.8.1"   scope="test"/>

Modified: 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java
===================================================================
--- 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java
    2016-07-21 12:02:46 UTC (rev 4877)
+++ 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java
    2016-08-07 16:04:12 UTC (rev 4878)
@@ -242,6 +242,21 @@
 //        return valueOf(result);
     }
     
+    private int tenthPower(){
+       return (int)Math.floor(Math.log10(doubleValue()))+1;
+    }
+    
+    public Rational roundSF(int significantFigures, Integer scale) {
+       int tenthPower = tenthPower();
+       int scale1 = significantFigures-tenthPower;
+       if(scale!=null){
+               scale1 = Math.max(scale.intValue(), scale1);            
+       }
+       
+        return valueOf(toBigDecimal(scale1, RoundingMode.HALF_UP));
+    }
+    
+    
     public BigDecimal toBigDecimal(int scale, RoundingMode roundingMode) {
         return isInteger() ? new BigDecimal(numerator) : new 
BigDecimal(numerator).divide(new BigDecimal(denominator), scale, roundingMode);
     }

Modified: 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java
===================================================================
--- 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java
        2016-07-21 12:02:46 UTC (rev 4877)
+++ 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java
        2016-08-07 16:04:12 UTC (rev 4878)
@@ -9,6 +9,14 @@
         assertEquals(Rational.tryParse("1234.57"), 
Rational.valueOf(1234.5678).round(2));
     }
     
+    
+    public void testRoundSF() throws ValueException {
+       assertEquals(Rational.tryParse("1234.57"), 
Rational.tryParse("1234.5678").roundSF(3,2)); 
+        assertEquals(Rational.tryParse("1230.00"), 
Rational.tryParse("1234.5678").roundSF(3,null));
+        assertEquals(Rational.tryParse("0.0568"), 
Rational.tryParse("0.056789").roundSF(3,null));
+    }
+    
+    
     public void testParse() throws ValueException {
        assertEquals(Rational.tryParse(".12"), Rational.tryParse("0.12"));
        assertEquals(Rational.tryParse("0."), Rational.tryParse("0.0"));

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to