-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### 方法一 | ||
|
||
正则表达式 | ||
|
||
```js | ||
export default function isAmount(amount) { | ||
return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isAmount(amount) { | ||
return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
金额合法性判断 | ||
|
||
合法的金额包含整数以及小数点后不超过两位的小数 | ||
|
||
用例 1: | ||
|
||
```js | ||
const amount = '20'; | ||
isAmount(amount); // true | ||
``` | ||
|
||
用例 2: | ||
|
||
```js | ||
const amount = '1.23'; | ||
isAmount(amount); // true | ||
``` | ||
|
||
用例 3: | ||
|
||
```js | ||
const amount = '1.'; | ||
isAmount(amount); // false | ||
``` | ||
|
||
用例 4: | ||
|
||
```js | ||
const amount = '0.2'; | ||
isAmount(amount); // true | ||
``` | ||
|
||
用例 5: | ||
|
||
```js | ||
const amount = '.1'; | ||
isAmount(amount); // false | ||
``` | ||
|
||
用例 6: | ||
|
||
```js | ||
const amount = '02'; | ||
isAmount(amount); // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import isAmount from './answer.mjs'; | ||
import { it } from 'mocha'; | ||
import { assert } from 'chai'; | ||
|
||
it('用例 1: 输入 1.23', () => { | ||
assert.equal(isAmount('1.23'), true); | ||
}); | ||
|
||
it('用例 2: 输入 256.23', () => { | ||
assert.equal(isAmount('256.23'), true); | ||
}); | ||
|
||
it('用例 3: 输入 0.23', () => { | ||
assert.equal(isAmount('0.23'), true); | ||
}); | ||
|
||
it('用例 4: 输入 0.2', () => { | ||
assert.equal(isAmount('0.2'), true); | ||
}); | ||
|
||
it('用例 5: 输入 0.234', () => { | ||
assert.equal(isAmount('0.234'), false); | ||
}); | ||
|
||
it('用例 6: 输入 0.', () => { | ||
assert.equal(isAmount('0.'), false); | ||
}); | ||
|
||
it('用例 7: 输入 .2', () => { | ||
assert.equal(isAmount('.2'), false); | ||
}); | ||
|
||
it('用例 8: 输入 20', () => { | ||
assert.equal(isAmount('20'), true); | ||
}); | ||
|
||
it('用例 9: 输入 02', () => { | ||
assert.equal(isAmount('02'), false); | ||
}); | ||
|
||
it('用例 10: 输入 102', () => { | ||
assert.equal(isAmount('102'), true); | ||
}); | ||
|
||
it('用例 11: 输入 102.2', () => { | ||
assert.equal(isAmount('102.2'), true); | ||
}); |