Author: brentworden
Date: Fri Feb  4 22:24:20 2005
New Revision: 151480

URL: http://svn.apache.org/viewcvs?view=rev&rev=151480
Log:
added rounding methods.

Modified:
    
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
    
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
    jakarta/commons/proper/math/trunk/xdocs/changes.xml
    jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml

Modified: 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?view=diff&r1=151479&r2=151480
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
 (original)
+++ 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
 Fri Feb  4 22:24:20 2005
@@ -16,6 +16,8 @@
 
 package org.apache.commons.math.util;
 
+import java.math.BigDecimal;
+
 /**
  * Some useful additions to the built-in functions in [EMAIL PROTECTED] Math}.
  *
@@ -45,6 +47,59 @@
      * Private Constructor
      */
     private MathUtils() {
+    }
+    
+    /**
+     * Round the given value to the specified number of decimal places.  The
+     * value is rounded using the [EMAIL PROTECTED] BigDecimal#ROUND_HALF_UP} 
method.
+     * @param x the value to round.
+     * @param scale the number of digits to the right of the decimal point.
+     * @return the rounded value.
+     */
+    public static double round(double x, int scale) {
+        return round(x, scale, BigDecimal.ROUND_HALF_UP);
+    }
+
+    /**
+     * Round the given value to the specified number of decimal places.  The
+     * value is rounded using the given method which is any method defined in
+     * [EMAIL PROTECTED] BigDecimal}.
+     * @param x the value to round.
+     * @param scale the number of digits to the right of the decimal point.
+     * @param roundingMethod the rounding method as defined in
+     *        [EMAIL PROTECTED] BigDecimal}. 
+     * @return the rounded value.
+     */
+    public static double round(
+        double x, int scale, int roundingMethod)
+    {
+        return (new BigDecimal(x).setScale(scale, roundingMethod))
+            .doubleValue();
+    }
+    
+    /**
+     * Round the given value to the specified number of decimal places.  The
+     * value is rounding using the [EMAIL PROTECTED] BigDecimal#ROUND_HALF_UP} 
method.
+     * @param x the value to round.
+     * @param scale the number of digits to the right of the decimal point.
+     * @return the rounded value.
+     */
+    public static float round(float x, int scale) {
+        return round(x, scale, BigDecimal.ROUND_HALF_UP);
+    }
+
+    /**
+     * Round the given value to the specified number of decimal places.  The
+     * value is rounded using the given method which is any method defined in
+     * [EMAIL PROTECTED] BigDecimal}.
+     * @param x the value to round.
+     * @param scale the number of digits to the right of the decimal point.
+     * @param roundingMethod the rounding method as defined in
+     *        [EMAIL PROTECTED] BigDecimal}. 
+     * @return the rounded value.
+     */
+    public static float round(float x, int scale, int roundingMethod) {
+        return (new BigDecimal(x).setScale(scale, 
roundingMethod)).floatValue();
     }
     
     /**

Modified: 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java?view=diff&r1=151479&r2=151480
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
 (original)
+++ 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
 Fri Feb  4 22:24:20 2005
@@ -15,6 +15,10 @@
  */
 package org.apache.commons.math.util;
 
+import java.math.BigDecimal;
+
+import org.apache.commons.math.TestUtils;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -425,5 +429,27 @@
         assertEquals(150, MathUtils.lcm(-a, b));
         assertEquals(150, MathUtils.lcm(a, -b));
         assertEquals(2310, MathUtils.lcm(a, c));
+    }
+    
+    public void testRoundFloat() {
+        float x = 1.234567890f;
+        assertEquals(1.23f, MathUtils.round(x, 2), 0.0f);
+        assertEquals(1.235f, MathUtils.round(x, 3), 0.0f);
+        assertEquals(1.2346f, MathUtils.round(x, 4), 0.0f);
+
+        assertEquals(1.23f, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 
0.0f);
+        assertEquals(1.234f, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 
0.0f);
+        assertEquals(1.2345f, MathUtils.round(x, 4, BigDecimal.ROUND_DOWN), 
0.0f);
+    }
+    
+    public void testRoundDouble() {
+        double x = 1.234567890;
+        assertEquals(1.23, MathUtils.round(x, 2), 0.0);
+        assertEquals(1.235, MathUtils.round(x, 3), 0.0);
+        assertEquals(1.2346, MathUtils.round(x, 4), 0.0);
+
+        assertEquals(1.23, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 0.0);
+        assertEquals(1.234, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 0.0);
+        assertEquals(1.2345, MathUtils.round(x, 4, BigDecimal.ROUND_DOWN), 
0.0);
     }
 }

Modified: jakarta/commons/proper/math/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/changes.xml?view=diff&r1=151479&r2=151480
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/changes.xml Fri Feb  4 22:24:20 2005
@@ -39,6 +39,9 @@
   <body>
     <release version="1.1" date="In Development"  
        description="Jakarta Commons Math 1.1 - Development">
+      <action dev="brentworden" type="add">
+        Added convience methods for rounding.
+      </action>
       <action dev="brentworden" type="add" due-to="C. Scott Ananian">
         Added Fraction class based on commons-lang implementation.  With the
         fraction class, FractionFormat and ProperFractionFormat classes were

Modified: jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml?view=diff&r1=151479&r2=151480
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml Fri Feb  4 
22:24:20 2005
@@ -17,7 +17,7 @@
   -->
   
 <?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
-<!-- $Revision: 1.9 $ $Date: 2004/07/20 22:29:50 $ -->
+<!-- $Revision: 1.9 $ $Date$ -->
 <document url="utilities.html">
 
 <properties>
@@ -164,7 +164,14 @@
     <li>
     a hash function, <code>hash(double),</code> returning a long-valued
     hash code for a double value.
-    </li></ul>
+    </li>
+    <li>
+    Convience methods to round floating-point number to arbitrary precision.
+    </li>
+    <li>
+    Least common multiple and greatest common denominator functions.
+    </li>
+    </ul>
     </p>
 </subsection>
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to