Added stringToDouble, stringToLong functions to NumberUtils.

Patch supplied by Fredrik Westermarck
Reviewd by Phil Steitz


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137664 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-05 15:55:09 +00:00
parent 2546311b27
commit 69b8f445d7
2 changed files with 122 additions and 4 deletions

View File

@ -71,7 +71,7 @@ import org.apache.commons.lang.StringUtils;
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
* @since 2.0
* @version $Id: NumberUtils.java,v 1.11 2003/09/04 07:27:12 psteitz Exp $
* @version $Id: NumberUtils.java,v 1.12 2003/09/05 15:55:09 psteitz Exp $
*/
public class NumberUtils {
@ -155,6 +155,40 @@ public class NumberUtils {
}
}
/**
* <p>Convert a <code>String</code> to a <code>long</code>, returning
* <code>zero</code> if the conversion fails.</p>
*
* <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
*
* @param str the string to convert, may be null
* @return the long represented by the string, or <code>0</code> if
* conversion fails
* @since 2.1
*/
public static long stringToLong(String str) {
return stringToLong(str, 0L);
}
/**
* <p>Convert a <code>String</code> to a <code>long</code>, returning a
* default value if the conversion fails.</p>
*
* <p>If the string is <code>null</code>, the default value is returned.</p>
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the long represented by the string, or the default if conversion fails
* @since 2.1
*/
public static long stringToLong(String str, long defaultValue) {
try {
return Long.parseLong(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
/**
* <p>Convert a <code>String</code> to a <code>float</code>, returning
* <code>0.0f</code> if the conversion fails.</p>
@ -187,8 +221,7 @@ public class NumberUtils {
public static float stringToFloat(String str, float defaultValue) {
if(str==null) {
return defaultValue;
}
}
try {
return Float.parseFloat(str);
} catch (NumberFormatException nfe) {
@ -196,6 +229,46 @@ public class NumberUtils {
}
}
/**
* <p>Convert a <code>String</code> to a <code>double</code>, returning
* <code>0.0d</code> if the conversion fails.</p>
*
* <p>If the string <code>str</code> is <code>null</code>,
* <code>0.0d</code> is returned.</p>
*
* @param str the string to convert, may be <code>null</code>
* @return the double represented by the string, or <code>0.0d</code>
* if conversion fails
* @since 2.1
*/
public static double stringToDouble(String str) {
return stringToDouble(str, 0.0d);
}
/**
* <p>Convert a <code>String</code> to a <code>double</code>, returning a
* default value if the conversion fails.</p>
*
* <p>If the string <code>str</code> is <code>null</code>, the default
* value is returned.</p>
*
* @param str the string to convert, may be <code>null</code>
* @param defaultValue the default value
* @return the double represented by the string, or defaultValue
* if conversion fails
* @since 2.1
*/
public static double stringToDouble(String str, double defaultValue) {
if(str==null) {
return defaultValue;
}
try {
return Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
//-----------------------------------------------------------------------
// must handle Long, Float, Integer, Float, Short,
// BigDecimal, BigInteger and Byte

View File

@ -75,7 +75,7 @@ import org.apache.commons.lang.SystemUtils;
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: NumberUtilsTest.java,v 1.8 2003/09/04 07:27:12 psteitz Exp $
* @version $Id: NumberUtilsTest.java,v 1.9 2003/09/05 15:55:09 psteitz Exp $
*/
public class NumberUtilsTest extends TestCase {
@ -123,6 +123,28 @@ public class NumberUtilsTest extends TestCase {
assertTrue("stringToInt(String,int) 2 failed", NumberUtils.stringToInt("1234.5", 5) == 5);
}
/**
* Test for long stringToLong(String)
*/
public void testStringToLongString() {
assertTrue("stringToLong(String) 1 failed", NumberUtils.stringToLong("12345") == 12345l);
assertTrue("stringToLong(String) 2 failed", NumberUtils.stringToLong("abc") == 0l);
assertTrue("stringToLong(String) 3 failed", NumberUtils.stringToLong("1L") == 0l);
assertTrue("stringToLong(String) 4 failed", NumberUtils.stringToLong("1l") == 0l);
assertTrue("stringToLong(Long.MAX_VALUE) failed", NumberUtils.stringToLong(Long.MAX_VALUE+"") == Long.MAX_VALUE);
assertTrue("stringToLong(Long.MIN_VALUE) failed", NumberUtils.stringToLong(Long.MIN_VALUE+"") == Long.MIN_VALUE);
assertTrue("stringToLong(empty) failed", NumberUtils.stringToLong("") == 0l);
assertTrue("stringToLong(null) failed", NumberUtils.stringToLong(null) == 0l);
}
/**
* Test for long stringToLong(String, long)
*/
public void testStringToLongStringL() {
assertTrue("stringToLong(String,long) 1 failed", NumberUtils.stringToLong("12345", 5l) == 12345l);
assertTrue("stringToLong(String,long) 2 failed", NumberUtils.stringToLong("1234.5", 5l) == 5l);
}
/**
* Test for float stringToFloat(String)
*/
@ -130,6 +152,8 @@ public class NumberUtilsTest extends TestCase {
assertTrue("stringToFloat(String) 1 failed", NumberUtils.stringToFloat("-1.2345") == -1.2345f);
assertTrue("stringToFloat(String) 2 failed", NumberUtils.stringToFloat("1.2345") == 1.2345f);
assertTrue("stringToFloat(String) 3 failed", NumberUtils.stringToFloat("abc") == 0.0f);
assertTrue("stringToFloat(Float.MAX_VALUE) failed", NumberUtils.stringToFloat(Float.MAX_VALUE+"") == Float.MAX_VALUE);
assertTrue("stringToFloat(Float.MIN_VALUE) failed", NumberUtils.stringToFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE);
assertTrue("stringToFloat(empty) failed", NumberUtils.stringToFloat("") == 0.0f);
assertTrue("stringToFloat(null) failed", NumberUtils.stringToFloat(null) == 0.0f);
}
@ -142,6 +166,27 @@ public class NumberUtilsTest extends TestCase {
assertTrue("stringToFloat(String,int) 2 failed", NumberUtils.stringToFloat("a", 5.0f) == 5.0f);
}
/**
* Test for double stringToDouble(String)
*/
public void testStringToDoubleString() {
assertTrue("stringToDouble(String) 1 failed", NumberUtils.stringToDouble("-1.2345") == -1.2345d);
assertTrue("stringToDouble(String) 2 failed", NumberUtils.stringToDouble("1.2345") == 1.2345d);
assertTrue("stringToDouble(String) 3 failed", NumberUtils.stringToDouble("abc") == 0.0d);
assertTrue("stringToDouble(Double.MAX_VALUE) failed", NumberUtils.stringToDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE);
assertTrue("stringToDouble(Double.MIN_VALUE) failed", NumberUtils.stringToDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE);
assertTrue("stringToDouble(empty) failed", NumberUtils.stringToDouble("") == 0.0d);
assertTrue("stringToDouble(null) failed", NumberUtils.stringToDouble(null) == 0.0d);
}
/**
* Test for double stringToFloat(String, float)
*/
public void testStringToDoubleStringD() {
assertTrue("stringToDouble(String,int) 1 failed", NumberUtils.stringToDouble("1.2345", 5.1d) == 1.2345d);
assertTrue("stringToDouble(String,int) 2 failed", NumberUtils.stringToDouble("a", 5.0d) == 5.0d);
}
public void testCreateNumber() {
//a lot of things can go wrong
assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));