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

This commit is contained in:
Rob Tompkins 2018-08-14 10:44:30 -04:00
parent 1deca6672d
commit 08aa21f921
2 changed files with 61 additions and 1 deletions

View File

@ -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

View File

@ -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)}.
*/