java判断是否是小数
内容摘要
可以将数字转化成String,然后判断里面有没有小数点,如果有,就是小数,没有就不是小数。public class JudgeNumber {
public static boolean judgeIsDecimal(String num)
public static boolean judgeIsDecimal(String num)
文章正文
可以将数字转化成String,然后判断里面有没有小数点,如果有,就是小数,没有就不是小数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class JudgeNumber { public static boolean judgeIsDecimal(String num){ boolean isdecimal = false ; if (num.contains( "." )) { isdecimal= true ; } return isdecimal; } public static void main(String[] args) { //测试的main方法 int num1 = 34; double num2=67.8; boolean is1 = judgeIsDecimal(String.valueOf(num1)); boolean is2=judgeIsDecimal(String.valueOf(num2)); System.out.println(is1); //fasle System.out.println(is2); //true } } |
contains(),该方法是判断字符串中是否有子字符串。如果有则返回true,如果没有则返回false。
更多java知识请关注java基础教程。
代码注释
[!--zhushi--]