經(jīng)常會遇到這樣的情況,需要判斷一個字符串是否是一個合法的數(shù),包括整數(shù),小數(shù)或者實數(shù)。
網(wǎng)上查到很多文章大多是判斷這個字符串是否全為數(shù)字,比如下面這段來自StringUtils的代碼,可以看到,13.2這樣的數(shù)字實際上會返回false,可是,他的確是一個數(shù)字。
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;
}
當然,網(wǎng)上還能查到很多其他方式,諸如用正則表達式判斷是否0-9,用字符ascii碼判斷是否是數(shù)字以及用Double.parseDouble()是否拋出異常來判斷是否為數(shù)字。
事實上,除了最后一種方式能達到我們的要求,其他的都很難真正做到類似的判斷。但是最后一種方式也很難區(qū)別出到底是正整數(shù),負整數(shù),正小數(shù)還是負小數(shù),而且,捕獲異常的方式實在是有些難看。
基于此原因,我自己寫了一個工具類,專門用作數(shù)的檢測,目前能夠檢測正整數(shù),負整數(shù),整數(shù),正小數(shù),負小數(shù),小數(shù)以及實數(shù),采用的仍然是正則表達式的方式,當然,如果有遺漏或者錯誤,歡迎聯(lián)系我以便更正,同時也歡迎修改或使用這些代碼以便符合你的應用場景。
可以簡單講下正則的思想以便修改,
1. 對于正整數(shù)而言,可以帶+號,第一個數(shù)字不能為0
2. 對于負整數(shù)而言,必須帶負號,第一個數(shù)字也不能為0
3. 對于整數(shù)而言,實際是由0,正整數(shù)和負整數(shù)組成的,所以偷個懶用前兩個方法一起判斷
4. 對于正小數(shù)而言,可以考帶+號,并考慮兩種情況,第一個數(shù)字為0和第一個數(shù)字不為0,第一個數(shù)字為0時,則小數(shù)點后面應該不為0,第一個數(shù)字不為0時,小數(shù)點后可以為任意數(shù)字
5. 對于負小數(shù)而言,必須帶負號,其余都同上
6. 對于小數(shù),可以帶正負號,并且?guī)?shù)點就行了,但是至少保證小數(shù)點有一邊不為空,所以這里還是分左邊不為空和右邊不為空的情況
7. 實數(shù)比較簡單,,要么是整數(shù),要么是小數(shù)
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"));
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- java基于正則表達式實現(xiàn)時間日期的常用判斷操作實例
- Java使用正則表達式判斷字符串是否以字符開始
- Java正則表達式判斷字符串中是否包含中文示例
- java使用正則表達式判斷郵箱格式是否正確的方法
- java正則表達式判斷前端參數(shù)修改表中另一個字段的值