isNumber(String) and createNumber(String) both modified to support "2.". LANG-521

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@893088 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-12-22 06:52:42 +00:00
parent 506bd018b3
commit 26bc3fe010
2 changed files with 15 additions and 1 deletions

View File

@ -488,7 +488,7 @@ public static Number createNumber(String str) throws NumberFormatException {
}
dec = null;
}
if (!Character.isDigit(lastChar)) {
if (!Character.isDigit(lastChar) && lastChar != '.') {
if (expPos > -1 && expPos < str.length() - 1) {
exp = str.substring(expPos + 1, str.length() - 1);
} else {
@ -1385,6 +1385,14 @@ public static boolean isNumber(String str) {
// can't have an E at the last byte
return false;
}
if (chars[i] == '.') {
if (hasDecPoint || hasExp) {
// two decimal points or dec in exponent
return false;
}
// single trailing decimal point after non-exponent is ok
return foundDigit;
}
if (!allowSigns
&& (chars[i] == 'd'
|| chars[i] == 'D'

View File

@ -208,6 +208,9 @@ public void testCreateNumber() {
.createNumber("10" + Integer.MAX_VALUE));
assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
.createNumber("10" + Long.MAX_VALUE));
// LANG-521
assertEquals("createNumber(String) LANG-521 failed", new Float("2."), NumberUtils.createNumber("2."));
}
public void testCreateFloat() {
@ -1130,6 +1133,9 @@ public void testIsNumber() {
assertTrue("isNumber(String) 24 Neg failed", !NumberUtils.isNumber(val));
assertTrue("isNumber(String)/createNumber(String) 24 Neg failed", !checkCreateNumber(val));
// LANG-521
val = "2.";
assertTrue("isNumber(String) LANG-521 failed", NumberUtils.isNumber(val));
}
private boolean checkCreateNumber(String val) {