//浮點(diǎn)型判斷
?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();
?}
浮點(diǎn)型測(cè)試用例:
?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判斷數(shù)字的資料有點(diǎn)不全(沒(méi)有浮點(diǎn)的判斷),發(fā)現(xiàn)有的還有錯(cuò)誤。所有自己就弄了一個(gè)做為筆記以后用。
可能自己的也有點(diǎn)測(cè)試不到位,但是我想到的測(cè)試用例,都測(cè)試通過(guò)。
posted on 2008-10-18 14:48
劉祥 閱讀(10351)
評(píng)論(6) 編輯 收藏