Test cases and (temporary?) fix for BZ 35904.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@226479 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2005-07-30 07:25:26 +00:00
parent c5a820aae2
commit cb85a3bc90
2 changed files with 21 additions and 2 deletions

View File

@ -75,7 +75,7 @@ public final class MathUtils {
public static double round( public static double round(
double x, int scale, int roundingMethod) double x, int scale, int roundingMethod)
{ {
return (new BigDecimal(x).setScale(scale, roundingMethod)) return (new BigDecimal(new Double(x).toString()).setScale(scale, roundingMethod))
.doubleValue(); .doubleValue();
} }
@ -103,7 +103,7 @@ public final class MathUtils {
* @since 1.1 * @since 1.1
*/ */
public static float round(float x, int scale, int roundingMethod) { public static float round(float x, int scale, int roundingMethod) {
return (new BigDecimal(x).setScale(scale, roundingMethod)).floatValue(); return (new BigDecimal(new Float(x).toString()).setScale(scale, roundingMethod)).floatValue();
} }
/** /**

View File

@ -477,6 +477,16 @@ public final class MathUtilsTest extends TestCase {
assertEquals(1.23f, MathUtils.round(x, 2), 0.0f); assertEquals(1.23f, MathUtils.round(x, 2), 0.0f);
assertEquals(1.235f, MathUtils.round(x, 3), 0.0f); assertEquals(1.235f, MathUtils.round(x, 3), 0.0f);
assertEquals(1.2346f, MathUtils.round(x, 4), 0.0f); assertEquals(1.2346f, MathUtils.round(x, 4), 0.0f);
// BZ 35904
assertEquals(30.1f, MathUtils.round(30.095f, 2), 0.0f);
assertEquals(30.1f, MathUtils.round(30.095f, 1), 0.0f);
assertEquals(50.09f, MathUtils.round(50.085f, 2), 0.0f);
assertEquals(50.19f, MathUtils.round(50.185f, 2), 0.0f);
assertEquals(50.01f, MathUtils.round(50.005f, 2), 0.0f);
assertEquals(30.01f, MathUtils.round(30.005f, 2), 0.0f);
assertEquals(30.65f, MathUtils.round(30.645f, 2), 0.0f);
assertEquals(1.23f, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 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.234f, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 0.0f);
@ -488,6 +498,15 @@ public final class MathUtilsTest extends TestCase {
assertEquals(1.23, MathUtils.round(x, 2), 0.0); assertEquals(1.23, MathUtils.round(x, 2), 0.0);
assertEquals(1.235, MathUtils.round(x, 3), 0.0); assertEquals(1.235, MathUtils.round(x, 3), 0.0);
assertEquals(1.2346, MathUtils.round(x, 4), 0.0); assertEquals(1.2346, MathUtils.round(x, 4), 0.0);
// BZ 35904
assertEquals(30.1d, MathUtils.round(30.095d, 2), 0.0d);
assertEquals(30.1d, MathUtils.round(30.095d, 1), 0.0d);
assertEquals(50.09d, MathUtils.round(50.085d, 2), 0.0d);
assertEquals(50.19d, MathUtils.round(50.185d, 2), 0.0d);
assertEquals(50.01d, MathUtils.round(50.005d, 2), 0.0d);
assertEquals(30.01d, MathUtils.round(30.005d, 2), 0.0d);
assertEquals(30.65d, MathUtils.round(30.645d, 2), 0.0d);
assertEquals(1.23, MathUtils.round(x, 2, BigDecimal.ROUND_DOWN), 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.234, MathUtils.round(x, 3, BigDecimal.ROUND_DOWN), 0.0);