您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Java中怎么判斷是否是整數,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
經常會遇到這樣的情況,需要判斷一個字符串是否是一個合法的數,包括整數,小數或者實數。
網上查到很多文章大多是判斷這個字符串是否全為數字,比如下面這段來自StringUtils的代碼,可以看到,13.2這樣的數字實際上會返回false,可是,他的確是一個數字。
public static boolean isNumeric(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if (Character.isDigit(str.charAt(i)) == false) { return false; } } return true; }
當然,網上還能查到很多其他方式,諸如用正則表達式判斷是否0-9,用字符ascii碼判斷是否是數字以及用Double.parseDouble()是否拋出異常來判斷是否為數字。
事實上,除了最后一種方式能達到我們的要求,其他的都很難真正做到類似的判斷。但是最后一種方式也很難區別出到底是正整數,負整數,正小數還是負小數,而且,捕獲異常的方式實在是有些難看。
基于此原因,我自己寫了一個工具類,專門用作數的檢測,目前能夠檢測正整數,負整數,整數,正小數,負小數,小數以及實數,采用的仍然是正則表達式的方式,當然,如果有遺漏或者錯誤,歡迎聯系我以便更正,同時也歡迎修改或使用這些代碼以便符合你的應用場景。
可以簡單講下正則的思想以便修改,
1. 對于正整數而言,可以帶+號,第一個數字不能為0
2. 對于負整數而言,必須帶負號,第一個數字也不能為0
3. 對于整數而言,實際是由0,正整數和負整數組成的,所以偷個懶用前兩個方法一起判斷
4. 對于正小數而言,可以考帶+號,并考慮兩種情況,第一個數字為0和第一個數字不為0,第一個數字為0時,則小數點后面應該不為0,第一個數字不為0時,小數點后可以為任意數字
5. 對于負小數而言,必須帶負號,其余都同上
6. 對于小數,可以帶正負號,并且帶小數點就行了,但是至少保證小數點有一邊不為空,所以這里還是分左邊不為空和右邊不為空的情況
7. 實數比較簡單,,要么是整數,要么是小數
package com.sap.cesp.creditinsight.web.app.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class NumberValidationUtils { private static boolean isMatch(String regex, String orginal){ if (orginal == null || orginal.trim().equals("")) { return false; } Pattern pattern = Pattern.compile(regex); Matcher isNum = pattern.matcher(orginal); return isNum.matches(); } public static boolean isPositiveInteger(String orginal) { return isMatch("^\\+{0,1}[1-9]\\d*", orginal); } public static boolean isNegativeInteger(String orginal) { return isMatch("^-[1-9]\\d*", orginal); } public static boolean isWholeNumber(String orginal) { return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal); } public static boolean isPositiveDecimal(String orginal){ return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal); } public static boolean isNegativeDecimal(String orginal){ return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal); } public static boolean isDecimal(String orginal){ return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal); } public static boolean isRealNumber(String orginal){ return isWholeNumber(orginal) || isDecimal(orginal); } }
測試用例如下:
package com.sap.cesp.creditinsight.web.app.util; import junit.framework.Assert; import org.junit.Test; public class NumberValidationUtilsTest { /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isPositiveInteger(java.lang.String)} */ //correct test case: 1, 87653521123567 //wrong test case: 0.1, 0, 0123, -1, -0.1, ab @Test public void testIsPositiveInteger() { Assert.assertTrue(NumberValidationUtils.isPositiveInteger("1")); Assert.assertTrue(NumberValidationUtils.isPositiveInteger("+12")); Assert.assertTrue(NumberValidationUtils.isPositiveInteger("87653521123567")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0.1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0123")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-0.1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isNegativeInteger(java.lang.String)} */ //correct test case: -1, -87653521123567 //wrong test case: 0.1, 0, 0123, 1, -0.1, -ab @Test public void testIsNegativeInteger() { Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-1")); Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-87653521123567")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0.1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0123")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("-0.1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isWholeNumber(java.lang.String)}. */ //correct test case: -1, 0, 1, 8673434231, -282464334 //wrong test case: 0.1, 0123, -0.1, ab @Test public void testIsWholeNumber() { Assert.assertTrue(NumberValidationUtils.isWholeNumber("-1")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("0")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("1")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("+12")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("8673434231")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("-282464334")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("0123")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("0.1")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("-0.1")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isPositiveDecimal(java.lang.String)} */ //correct test case: 0.1, 0.132213, 1.0 //wrong test case: 1, 0.0, 0123, -1, -0.1 @Test public void testIsPositiveDecimal() { Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.1")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.132213")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("30.00")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("+12.0")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("1")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0.0")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-1")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-0.1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isNegativeDecimal(java.lang.String)} */ //correct test case: -0.132213, -1.0 //wrong test case: 1, 0, 0123, -1, 0.1 @Test public void testIsNegativeDecimal() { Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-0.132213")); Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-1.0")); Assert.assertTrue(NumberValidationUtils.isDecimal("-0.")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("1")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.0")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("-1")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isDecimal(java.lang.String)}. */ //correct test case: 0.1, 0.00, -0.132213 //wrong test case: 1, 0, 0123, -1, 0., ba @Test public void testIsDecimal() { Assert.assertTrue(NumberValidationUtils.isDecimal("0.1")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.00")); Assert.assertTrue(NumberValidationUtils.isDecimal("+0.0")); Assert.assertTrue(NumberValidationUtils.isDecimal("-0.132213")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.")); Assert.assertFalse(NumberValidationUtils.isDecimal("1")); Assert.assertFalse(NumberValidationUtils.isDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isDecimal("0")); Assert.assertFalse(NumberValidationUtils.isDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isDecimal("-1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isRealNumber(java.lang.String)}. */ //correct test case: 0.032213, -0.234, 0.0, 1, -1, 0 //wrong test case: 00.13, ab, +0.14 @Test public void testIsRealNumber() { Assert.assertTrue(NumberValidationUtils.isRealNumber("0.032213")); Assert.assertTrue(NumberValidationUtils.isRealNumber("-0.234")); Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0")); Assert.assertTrue(NumberValidationUtils.isRealNumber("1")); Assert.assertTrue(NumberValidationUtils.isRealNumber("+0.14")); Assert.assertTrue(NumberValidationUtils.isRealNumber("-1")); Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0")); Assert.assertFalse(NumberValidationUtils.isRealNumber("00.13")); Assert.assertFalse(NumberValidationUtils.isRealNumber("ab")); } }
看完上述內容,你們對Java中怎么判斷是否是整數有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。