Skip to content

Commit 120c6e5

Browse files
authored
fix: digitalToCash少于两位小数时需加个0 (#26)
1 parent 967da0d commit 120c6e5

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mlz/doraemon",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"description": "项目常用但lodash又没有的工具函数集合,哆啦A梦般方便实用",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/digitalToCash.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const digitalToCash = (target:number):string => {
1414
// 保留两位小数
1515
decimal = temp.substring(decimalPoint).substr(0, 3);
1616
temp = temp.substring(0, decimalPoint);
17+
// 如果小数点后只有一位数
18+
decimal = decimal.length < 3 ? `${decimal}0` : decimal;
1719
}
1820
while(temp.length > 0) {
1921
// 第一次循环无需加,

test/digitalToCash.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ describe('digitalToCash', () => {
1313
it('1000 => 1,000.00', () => {
1414
expect(digitalToCash(1000)).toBe('1,000.00');
1515
});
16-
it('100000000 => 100,000,000.00', () => {
17-
expect(digitalToCash(100000000)).toBe('100,000,000.00');
18-
});
1916
it('-100000000 => -100,000,000.00', () => {
2017
expect(digitalToCash(-100000000)).toBe('-100,000,000.00');
2118
});
2219
it('-9999.999 => -9,999.99', () => {
2320
expect(digitalToCash(-9999.999)).toBe('-9,999.99');
2421
});
22+
it('-9999.9 => -9,999.90', () => {
23+
expect(digitalToCash(-9999.9)).toBe('-9,999.90');
24+
});
2525
});

0 commit comments

Comments
 (0)