Added stringToFloat to NumberUtils

Patch contributed by Fredrik Westermarck
Reviewed by Phil Steitz


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137661 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-04 07:27:12 +00:00
parent 8822731cb3
commit e0bee37d15
2 changed files with 63 additions and 2 deletions

View File

@ -69,8 +69,9 @@ import org.apache.commons.lang.StringUtils;
* @author Phil Steitz
* @author Matthew Hawthorne
* @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.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 {
}
}
/**
* <p>Convert a <code>String</code> to a <code>float</code>, returning
* <code>0.0f</code> if the conversion fails.</p>
*
* <p>If the string <code>str</code> is <code>null</code>,
* <code>0.0f</code> is returned.</p>
*
* @param str the string to convert, may be <code>null</code>
* @return the float represented by the string, or <code>0.0f</code>
* if conversion fails
* @since 2.1
*/
public static float stringToFloat(String str) {
return stringToFloat(str, 0.0f);
}
/**
* <p>Convert a <code>String</code> to a <code>float</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 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

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.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"));