From 08aa21f9217df0a28d48be5be5db03ddf4867140 Mon Sep 17 00:00:00 2001 From: Rob Tompkins Date: Tue, 14 Aug 2018 10:44:30 -0400 Subject: [PATCH] LANG-1408: add toDouble(BigDecimal), toDouble(BigDecimal, double) --- .../commons/lang3/math/NumberUtils.java | 42 +++++++++++++++++++ .../commons/lang3/math/NumberUtilsTest.java | 20 ++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index 63dd8b3b6..5d17a5b39 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -67,6 +67,7 @@ public class NumberUtils { /** Reusable Float constant for minus one. */ public static final Float FLOAT_MINUS_ONE = Float.valueOf(-1.0f); + /** *

NumberUtils instances should NOT be constructed in standard programming. * Instead, the class should be used as NumberUtils.toInt("6");.

@@ -281,6 +282,47 @@ public class NumberUtils { } } + /** + *

Convert a BigDecimal to a double.

+ * + *

If the BigDecimal value is + * null, then the specified default value is returned.

+ * + *
+     *   NumberUtils.toDouble(null)                     = 0.0d
+     *   NumberUtils.toDouble(BigDecimal.valudOf(8.5d)) = 8.5d
+     * 
+ * + * @param value the BigDecimal to convert, may be null. + * @return the double represented by the BigDecimal or + * 0.0d if the BigDecimal is null. + * @since 3.8 + */ + public static double toDouble(BigDecimal value) { + return toDouble(value, 0.0d); + } + + /** + *

Convert a BigDecimal to a double.

+ * + *

If the BigDecimal value is + * null, then the specified default value is returned.

+ * + *
+     *   NumberUtils.toDouble(null, 1.1d)                     = 1.1d
+     *   NumberUtils.toDouble(BigDecimal.valudOf(8.5d), 1.1d) = 8.5d
+     * 
+ * + * @param value the BigDecimal to convert, may be null. + * @param defaultValue the default value + * @return the double represented by the BigDecimal or the + * defaultValue if the BigDecimal is null. + * @since 3.8 + */ + public static double toDouble(BigDecimal value, double defaultValue) { + return value == null ? defaultValue : value.doubleValue(); + } + //----------------------------------------------------------------------- /** *

Convert a String to a byte, returning diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index b0ed8bb0b..c81976502 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -163,7 +163,7 @@ public class NumberUtilsTest { assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE); assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE); assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d); - assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d); + assertTrue("toDouble(null) failed", NumberUtils.toDouble((String) null) == 0.0d); } /** @@ -180,6 +180,24 @@ public class NumberUtilsTest { assertTrue("toDouble(String,int) 7 failed", NumberUtils.toDouble("000.00", 5.1d) == 0d); } + /** + * Test for {@link NumberUtils#toDouble(BigDecimal)} + */ + @Test + public void testBigIntegerToDoubleBigInteger() { + assertTrue("toDouble(BigInteger) 1 failed", NumberUtils.toDouble((BigDecimal) null) == 0.0d); + assertTrue("toDouble(BigInteger) 2 failed", NumberUtils.toDouble(BigDecimal.valueOf(8.5d)) == 8.5d); + } + + /** + * Test for {@link NumberUtils#toDouble(BigDecimal, double)} + */ + @Test + public void testBigIntegerToDoubleBigIntegerD() { + assertTrue("toDouble(BigInteger) 1 failed", NumberUtils.toDouble((BigDecimal) null, 1.1d) == 1.1d); + assertTrue("toDouble(BigInteger) 2 failed", NumberUtils.toDouble(BigDecimal.valueOf(8.5d), 1.1d) == 8.5d); + } + /** * Test for {@link NumberUtils#toByte(String)}. */