add null check

This commit is contained in:
Looly 2022-05-27 18:03:50 +08:00
parent 400c74e82a
commit 705f65a207
2 changed files with 32 additions and 18 deletions

View File

@ -35,7 +35,7 @@ import java.util.Set;
* <ul>
* <li><a href="https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking">one-jdk7-bug-thinking</a></li>
* </ul>
*
* <p>
* TODO 需整理精简方法去掉无用的重载方法
*
* @author Looly
@ -1242,11 +1242,13 @@ public class NumberUtil {
* @return 是否为整数
*/
public static boolean isInteger(final String s) {
if(StrUtil.isNotBlank(s)) {
try {
Integer.parseInt(s);
} catch (final NumberFormatException e) {
return false;
}
}
return true;
}
@ -1259,11 +1261,13 @@ public class NumberUtil {
* @since 4.0.0
*/
public static boolean isLong(final String s) {
if (StrUtil.isNotBlank(s)) {
try {
Long.parseLong(s);
} catch (final NumberFormatException e) {
return false;
}
}
return true;
}
@ -1274,12 +1278,14 @@ public class NumberUtil {
* @return 是否为{@link Double}类型
*/
public static boolean isDouble(final String s) {
if (StrUtil.isNotBlank(s)) {
try {
Double.parseDouble(s);
return s.contains(".");
} catch (final NumberFormatException ignore) {
// ignore
}
}
return false;
}

View File

@ -450,7 +450,15 @@ public class NumberUtilTest {
@Test
public void divIntegerTest(){
System.out.println(NumberUtil.div(100101300, (Number) 100));
final BigDecimal div = NumberUtil.div(100101300, (Number) 100);
Assert.assertEquals(1001013, div.intValue());
}
@Test
public void isDoubleTest(){
Assert.assertFalse(NumberUtil.isDouble(null));
Assert.assertFalse(NumberUtil.isDouble(""));
Assert.assertFalse(NumberUtil.isDouble(" "));
}
}