java 正則表達式整形浮點型的判定


 //浮點型判斷
 public static boolean isDecimal(String str) {
  if(str==null || "".equals(str))
   return false;  
  java.util.regex.Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
  return pattern.matcher(str).matches();
 }

 //整型判斷
 public static boolean isInteger(String str){
  if(str==null )
   return false;
  Pattern pattern = Pattern.compile("[0-9]+");
  return pattern.matcher(str).matches();
 }

浮點型測試用例:
 public void testIsDecimal() {
  
  assertTrue("123",Test.isDecimal("1"));
  assertTrue("12.3",Test.isDecimal("12.3"));
  assertTrue(".123",Test.isDecimal(".123"));
  assertTrue("123.",Test.isDecimal("123."));
  
  assertFalse("",Test.isDecimal(""));
  assertFalse("null",Test.isDecimal(null));
  assertFalse("abc", Test.isDecimal("abc"));
  assertFalse("123abc", Test.isDecimal("123abc"));
  assertFalse("abc123", Test.isDecimal("abc123"));
  assertFalse("123.2.2", Test.isDecimal("123.2.2"));
  
 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM