Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 40606 invoked from network); 5 Feb 2005 06:24:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 5 Feb 2005 06:24:28 -0000 Received: (qmail 43599 invoked by uid 500); 5 Feb 2005 06:24:25 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 43569 invoked by uid 500); 5 Feb 2005 06:24:25 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 43553 invoked by uid 500); 5 Feb 2005 06:24:25 -0000 Received: (qmail 43550 invoked by uid 99); 5 Feb 2005 06:24:25 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Fri, 04 Feb 2005 22:24:25 -0800 Received: (qmail 40562 invoked by uid 65534); 5 Feb 2005 06:24:24 -0000 Message-ID: <20050205062424.40560.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Sat, 05 Feb 2005 06:24:23 -0000 Subject: svn commit: r151480 - in jakarta/commons/proper/math/trunk: src/java/org/apache/commons/math/util/MathUtils.java src/test/org/apache/commons/math/util/MathUtilsTest.java xdocs/changes.xml xdocs/userguide/utilities.xml To: commons-cvs@jakarta.apache.org From: brentworden@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: brentworden Date: Fri Feb 4 22:24:20 2005 New Revision: 151480 URL: http://svn.apache.org/viewcvs?view=3Drev&rev=3D151480 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/mat= h/util/MathUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/ja= va/org/apache/commons/math/util/MathUtils.java?view=3Ddiff&r1=3D151479&r2= =3D151480 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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 @@ =20 package org.apache.commons.math.util; =20 +import java.math.BigDecimal; + /** * Some useful additions to the built-in functions in {@link Math}. * @@ -45,6 +47,59 @@ * Private Constructor */ private MathUtils() { + } + =20 + /** + * Round the given value to the specified number of decimal places. T= he + * value is rounded using the {@link 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. T= he + * value is rounded using the given method which is any method defined= in + * {@link 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 + * {@link BigDecimal}.=20 + * @return the rounded value. + */ + public static double round( + double x, int scale, int roundingMethod) + { + return (new BigDecimal(x).setScale(scale, roundingMethod)) + .doubleValue(); + } + =20 + /** + * Round the given value to the specified number of decimal places. T= he + * value is rounding using the {@link 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. T= he + * value is rounded using the given method which is any method defined= in + * {@link 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 + * {@link BigDecimal}.=20 + * @return the rounded value. + */ + public static float round(float x, int scale, int roundingMethod) { + return (new BigDecimal(x).setScale(scale, roundingMethod)).floatVa= lue(); } =20 /** Modified: jakarta/commons/proper/math/trunk/src/test/org/apache/commons/mat= h/util/MathUtilsTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/te= st/org/apache/commons/math/util/MathUtilsTest.java?view=3Ddiff&r1=3D151479&= r2=3D151480 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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; =20 +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)); + } + =20 + public void testRoundFloat() { + float x =3D 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=2E0f); + assertEquals(1.234f, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN),= 0.0f); + assertEquals(1.2345f, MathUtils.round(x, 4, BigDecimal.ROUND_DOWN)= , 0.0f); + } + =20 + public void testRoundDouble() { + double x =3D 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=2E0); + 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=3Ddiff&r1=3D151479&r2=3D151480 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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 @@ + + Added convience methods for rounding. + Added Fraction class based on commons-lang implementation. With t= he fraction class, FractionFormat and ProperFractionFormat classes we= re 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=3Ddiff&r1=3D151479&r2=3D151480 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml (origin= al) +++ jakarta/commons/proper/math/trunk/xdocs/userguide/utilities.xml Fri Feb= 4 22:24:20 2005 @@ -17,7 +17,7 @@ --> =20 - + =20 @@ -164,7 +164,14 @@
  • a hash function, hash(double), returning a long-valued hash code for a double value. -
  • + +
  • + Convience methods to round floating-point number to arbitrary precisio= n=2E +
  • +
  • + Least common multiple and greatest common denominator functions. +
  • +

    =20 --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org