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:
parent
506bd018b3
commit
26bc3fe010
|
@ -488,7 +488,7 @@ public class NumberUtils {
|
|||
}
|
||||
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 class NumberUtils {
|
|||
// 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'
|
||||
|
|
|
@ -208,6 +208,9 @@ public class NumberUtilsTest extends TestCase {
|
|||
.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 class NumberUtilsTest extends TestCase {
|
|||
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) {
|
||||
|
|
Loading…
Reference in New Issue