LANG-1016: NumberUtils#isParsable method(s). Thanks to Juan Pablo Santos RodrÃguez.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1609273 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 11f51948..c279744 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
+ <action issue="LANG-1016" type="add" dev="britter" due-to="Juan Pablo Santos Rodríguez">NumberUtils#isParsable method(s)</action>
<action issue="LANG-1017" type="update" dev="britter" due-to="Christoph Schneegans">Use non-ASCII digits in Javadoc examples for StringUtils.isNumeric</action>
<action issue="LANG-1008" type="update" dev="britter" due-to="Thiago Andrade">Change min/max methods in NumberUtils/IEEE754rUtils from array input parameters to varargs</action>
<action issue="LANG-999" type="add" dev="britter" due-to="Ben Ripkens">Add fuzzy String matching logic to StringUtils</action>
diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index 602fabe..5a4f4c8 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -1466,5 +1466,28 @@
// found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
return !allowSigns && foundDigit;
}
+
+ /**
+ * <p>Checks whether the String is a parseable number.</p>
+ *
+ * <p>Parseable numbers include those Strings understood by <code>Integer.parseInt(String)</code>,
+ * <code>Long.parseLong(String)</code>, <code>Float.parseFloat(String)</code> or
+ * <code>Double.parseDouble(String)</code>.</p>
+ *
+ * <p>Hexadecimal and scientific notations are <strong>not</strong> considered parseable.
+ * See {@link #isNumber(String)} on those cases.</p>
+ *
+ * <p><code>Null</code> and empty String will return <code>false</code>.</p>
+ *
+ * @param str the <code>String</code> to check.
+ * @return <code>true</code> if the string is a parseable number.
+ * @since 3.4
+ */
+ public static boolean isParsable(final String str) {
+ if( StringUtils.endsWith( str, "." ) ) {
+ return false;
+ }
+ return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) );
+ }
}
diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
index 69cd3a5..96b7a73 100644
--- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
@@ -1254,6 +1254,22 @@
}
fail("Expecting "+ expected + " for isNumber/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
}
+
+ @Test
+ public void testIsParsable() {
+ assertFalse( NumberUtils.isParsable(null) );
+ assertFalse( NumberUtils.isParsable("") );
+ assertFalse( NumberUtils.isParsable("0xC1AB") );
+ assertFalse( NumberUtils.isParsable("65CBA2") );
+ assertFalse( NumberUtils.isParsable("pendro") );
+ assertFalse( NumberUtils.isParsable("64,2") );
+ assertFalse( NumberUtils.isParsable("64.2.2") );
+ assertFalse( NumberUtils.isParsable("64.") );
+ assertFalse( NumberUtils.isParsable("64L") );
+ assertTrue( NumberUtils.isParsable("64.2") );
+ assertTrue( NumberUtils.isParsable("64") );
+ assertTrue(NumberUtils.isParsable("018"));
+ }
private boolean checkCreateNumber(final String val) {
try {