Repository: commons-lang
Updated Branches:
  refs/heads/master 1deca6672 -> 08aa21f92


LANG-1408: add toDouble(BigDecimal), toDouble(BigDecimal, double)


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/08aa21f9
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/08aa21f9
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/08aa21f9

Branch: refs/heads/master
Commit: 08aa21f9217df0a28d48be5be5db03ddf4867140
Parents: 1deca66
Author: Rob Tompkins <chtom...@apache.org>
Authored: Tue Aug 14 10:44:30 2018 -0400
Committer: Rob Tompkins <chtom...@apache.org>
Committed: Tue Aug 14 10:44:30 2018 -0400

----------------------------------------------------------------------
 .../apache/commons/lang3/math/NumberUtils.java  | 42 ++++++++++++++++++++
 .../commons/lang3/math/NumberUtilsTest.java     | 20 +++++++++-
 2 files changed, 61 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/08aa21f9/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
----------------------------------------------------------------------
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 class NumberUtils {
       }
     }
 
+    /**
+     * <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

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/08aa21f9/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
----------------------------------------------------------------------
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 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)}.
      */

Reply via email to