This commit is contained in:
Looly
2022-05-27 18:06:54 +08:00
parent bfa130b564
commit c72ff2c292
3 changed files with 27 additions and 13 deletions

View File

@@ -1238,10 +1238,12 @@ public class NumberUtil {
* @return 是否为整数
*/
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
if(StrUtil.isNotBlank(s)) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
@@ -1255,10 +1257,12 @@ public class NumberUtil {
* @since 4.0.0
*/
public static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
if(StrUtil.isNotBlank(s)) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
@@ -1270,11 +1274,13 @@ public class NumberUtil {
* @return 是否为{@link Double}类型
*/
public static boolean isDouble(String s) {
try {
Double.parseDouble(s);
return s.contains(".");
} catch (NumberFormatException ignore) {
// ignore
if(StrUtil.isNotBlank(s)) {
try {
Double.parseDouble(s);
return s.contains(".");
} catch (NumberFormatException ignore) {
// ignore
}
}
return false;
}

View File

@@ -444,4 +444,11 @@ public class NumberUtilTest {
Assert.assertEquals(1001013, NumberUtil.div(100101300, (Number) 100).intValue());
}
@Test
public void isDoubleTest(){
Assert.assertFalse(NumberUtil.isDouble(null));
Assert.assertFalse(NumberUtil.isDouble(""));
Assert.assertFalse(NumberUtil.isDouble(" "));
}
}