-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNumericalCheck.ts
66 lines (58 loc) · 1.72 KB
/
NumericalCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export class NumericalCheck {
// 指针索引
private index = 0;
// 扫描无符号整数
private scanUnsignedInteger(str: string): boolean {
const before = this.index;
while (str.charAt(this.index) >= "0" && str.charAt(this.index) <= "9") {
this.index++;
}
return this.index > before;
}
// 扫描有符号整数
private scanInteger(str: string): boolean {
// 判断其是否包含正负号
if (str.charAt(this.index) == "+" || str.charAt(this.index) == "-") {
this.index++;
}
// 扫描无符号整数
return this.scanUnsignedInteger(str);
}
/**
* 判断字符串是否为数值类型
* @param numStr
*/
public isNumber(numStr: string): boolean {
if (numStr == null || numStr.length == 0) {
return false;
}
// 添加结束标志
numStr = numStr + "|";
// 跳过首部的空格
while (numStr.charAt(this.index) == " ") {
this.index++;
}
// 扫描整数部分
let numeric = this.scanInteger(numStr);
// 有小数点,处理小数部分
if (numStr.charAt(this.index) == ".") {
this.index++;
// 小数两边只要有一边有数字即可,所以用||
numeric = this.scanUnsignedInteger(numStr) || numeric;
}
// 有e||E,处理指数部分
if (numStr.charAt(this.index) == "E" || numStr.charAt(this.index) == "e") {
this.index++;
// e || E两边都要有数字,所以用&&
numeric = numeric && this.scanInteger(numStr);
}
// 跳过尾部空格
while (numStr.charAt(this.index) == " ") {
this.index++;
}
const checkResult = numeric && numStr.charAt(this.index) == "|";
// 重置指针索引
this.index = 0;
return checkResult;
}
}