You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/09/04 09:27:12 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java
psteitz 2003/09/04 00:27:12
Modified: lang/src/java/org/apache/commons/lang/math NumberUtils.java
lang/src/test/org/apache/commons/lang/math
NumberUtilsTest.java
Log:
Added stringToFloat to NumberUtils
Patch contributed by Fredrik Westermarck
Reviewed by Phil Steitz
Revision Changes Path
1.11 +43 -1 jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java
Index: NumberUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- NumberUtils.java 18 Aug 2003 02:22:24 -0000 1.10
+++ NumberUtils.java 4 Sep 2003 07:27:12 -0000 1.11
@@ -69,6 +69,7 @@
* @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$
*/
@@ -152,6 +153,47 @@
} 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>
+ *
+ * <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;
+ }
}
//-----------------------------------------------------------------------
1.8 +20 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
Index: NumberUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- NumberUtilsTest.java 18 Aug 2003 02:22:27 -0000 1.7
+++ NumberUtilsTest.java 4 Sep 2003 07:27:12 -0000 1.8
@@ -123,6 +123,25 @@
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"));