diff --git a/src/java/org/apache/commons/lang/math/NumberUtils.java b/src/java/org/apache/commons/lang/math/NumberUtils.java index bf6401be8..6b40a277b 100644 --- a/src/java/org/apache/commons/lang/math/NumberUtils.java +++ b/src/java/org/apache/commons/lang/math/NumberUtils.java @@ -69,8 +69,9 @@ import org.apache.commons.lang.StringUtils; * @author Phil Steitz * @author Matthew Hawthorne * @author Gary Gregory + * @author Fredrik Westermarck * @since 2.0 - * @version $Id: NumberUtils.java,v 1.10 2003/08/18 02:22:24 bayard Exp $ + * @version $Id: NumberUtils.java,v 1.11 2003/09/04 07:27:12 psteitz Exp $ */ public class NumberUtils { @@ -154,6 +155,47 @@ public class NumberUtils { } } + /** + *

Convert a String to a float, returning + * 0.0f if the conversion fails.

+ * + *

If the string str is null, + * 0.0f is returned.

+ * + * @param str the string to convert, may be null + * @return the float represented by the string, or 0.0f + * if conversion fails + * @since 2.1 + */ + public static float stringToFloat(String str) { + return stringToFloat(str, 0.0f); + } + + /** + *

Convert a String to a float, returning a + * default value if the conversion fails.

+ * + *

If the string str is null, the default + * value is returned.

+ * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the float represented by the string, or defaultValue + * if conversion fails + * @since 2.1 + */ + public static float stringToFloat(String str, float defaultValue) { + if(str==null) { + return defaultValue; + } + + try { + return Float.parseFloat(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } + } + //----------------------------------------------------------------------- // must handle Long, Float, Integer, Float, Short, // BigDecimal, BigInteger and Byte diff --git a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java index afb806d1a..4d55c47b2 100644 --- a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java +++ b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java @@ -75,7 +75,7 @@ import org.apache.commons.lang.SystemUtils; * @author Stephen Colebourne * @author Matthew Hawthorne * @author Gary Gregory - * @version $Id: NumberUtilsTest.java,v 1.7 2003/08/18 02:22:27 bayard Exp $ + * @version $Id: NumberUtilsTest.java,v 1.8 2003/09/04 07:27:12 psteitz Exp $ */ public class NumberUtilsTest extends TestCase { @@ -123,6 +123,25 @@ public class NumberUtilsTest extends TestCase { assertTrue("stringToInt(String,int) 2 failed", NumberUtils.stringToInt("1234.5", 5) == 5); } + /** + * Test for float stringToFloat(String) + */ + public void testStringToFloatString() { + 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(empty) failed", NumberUtils.stringToFloat("") == 0.0f); + assertTrue("stringToFloat(null) failed", NumberUtils.stringToFloat(null) == 0.0f); + } + + /** + * Test for float stringToFloat(String, float) + */ + public void testStringToFloatStringF() { + assertTrue("stringToFloat(String,int) 1 failed", NumberUtils.stringToFloat("1.2345", 5.1f) == 1.2345f); + assertTrue("stringToFloat(String,int) 2 failed", NumberUtils.stringToFloat("a", 5.0f) == 5.0f); + } + public void testCreateNumber() { //a lot of things can go wrong assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));