LANG-1408: add toDouble(BigDecimal), toDouble(BigDecimal, double)
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 63dd8b3..5d17a5b 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);
+
/**
* <p><code>NumberUtils</code> instances should NOT be constructed in standard programming.
* Instead, the class should be used as <code>NumberUtils.toInt("6");</code>.</p>
@@ -281,6 +282,47 @@ public static double toDouble(final String str, final double defaultValue) {
}
}
+ /**
+ * <p>Convert a <code>BigDecimal</code> to a <code>double</code>.</p>
+ *
+ * <p>If the <code>BigDecimal</code> <code>value</code> is
+ * <code>null</code>, then the specified default value is returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toDouble(null) = 0.0d
+ * NumberUtils.toDouble(BigDecimal.valudOf(8.5d)) = 8.5d
+ * </pre>
+ *
+ * @param value the <code>BigDecimal</code> to convert, may be <code>null</code>.
+ * @return the double represented by the <code>BigDecimal</code> or
+ * <code>0.0d</code> if the <code>BigDecimal</code> is <code>null</code>.
+ * @since 3.8
+ */
+ public static double toDouble(BigDecimal value) {
+ return toDouble(value, 0.0d);
+ }
+
+ /**
+ * <p>Convert a <code>BigDecimal</code> to a <code>double</code>.</p>
+ *
+ * <p>If the <code>BigDecimal</code> <code>value</code> is
+ * <code>null</code>, then the specified default value is returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toDouble(null, 1.1d) = 1.1d
+ * NumberUtils.toDouble(BigDecimal.valudOf(8.5d), 1.1d) = 8.5d
+ * </pre>
+ *
+ * @param value the <code>BigDecimal</code> to convert, may be <code>null</code>.
+ * @param defaultValue the default value
+ * @return the double represented by the <code>BigDecimal</code> or the
+ * defaultValue if the <code>BigDecimal</code> is <code>null</code>.
+ * @since 3.8
+ */
+ public static double toDouble(BigDecimal value, double defaultValue) {
+ return value == null ? defaultValue : value.doubleValue();
+ }
+
//-----------------------------------------------------------------------
/**
* <p>Convert a <code>String</code> to a <code>byte</code>, 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 b0ed8bb..c819765 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 void testStringToDoubleString() {
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 void testStringToDoubleStringD() {
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)}.
*/