//浮點型判斷
?public static boolean isDecimal(String str) {
??if(str==null || "".equals(str))
???return false;??
??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"));
??
?}
到google中找了下java判斷數字的資料有點不全(沒有浮點的判斷),發現有的還有錯誤。所有自己就弄了一個做為筆記以后用。
可能自己的也有點測試不到位,但是我想到的測試用例,都測試通過。
posted on 2008-10-18 14:48
劉祥 閱讀(10352)
評論(6) 編輯 收藏