Skip to content

Commit

Permalink
feat: add capitalizedAmount function (MrXujiang#45)
Browse files Browse the repository at this point in the history
Co-authored-by: 徐小夕 <[email protected]>
  • Loading branch information
ayangweb and MrXujiang authored Apr 30, 2023
1 parent e660768 commit 45096a7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"rules": {
"semi": "off",
"@typescript-eslint/no-explicit-any": "off",
"prettier/prettier": ["off", { "endOfLine": "auto" }],
"no-constant-condition": "off",
"@typescript-eslint/no-this-alias": "off"
}
},
"ignorePatterns": ["**/*.test.js"]
}
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"useTabs": false,
"semi": true,
"bracketSpacing": true,
"endOfLine": "auto",
"overrides": [
{
"files": ".prettierrc",
Expand Down
26 changes: 26 additions & 0 deletions src/capitalizedAmount/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: capitalizedAmount - 数字金额转为大写
nav:
title: 使用文档
path: /lib
group:
path: /math
title: 数学计算
order: 12
---

## capitalizedAmount

> 数字金额转为大写
>
> 注意:最大只支持到千亿
Demo:

```tsx | pure
import { capitalizedAmount } from 'xijs';

capitalizedAmount(100000000); // 壹亿元整
capitalizedAmount('2023.04'); // 贰仟零贰拾叁元肆分
capitalizedAmount(-1024); // 欠壹仟零贰拾肆元整
```
90 changes: 90 additions & 0 deletions src/capitalizedAmount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const capitalizedAmount = (amount: number | string) => {
try {
if (typeof amount === 'string') {
amount = parseFloat(amount.replaceAll(',', ''));
}

if (amount == null || isNaN(amount)) throw new Error('不是有效的金额!');

let result = '';

if (amount < 0) {
result = '欠';
amount = Math.abs(amount);
}

// 金额不能超过千亿以上
if (amount >= 10e11) throw new Error('计算金额过大!');

amount = String(amount.toFixed(2));

const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const units = ['', '拾', '佰', '仟'];
const bigUnits = ['', '万', '亿'];
const decimalUnits = ['角', '分'];

const amountArray = amount.split('.');
let integerPart: string | number[] = amountArray[0];
const decimalPart = amountArray[1];

// 整数部分
if (integerPart !== '0') {
integerPart = integerPart.split('').map(Number);

const levels = integerPart.reverse().reduce((prev, item, index) => {
const level = prev?.[0]?.length < 4 ? prev[0] : [];

const value = !item ? digits[item] : digits[item] + units[index % 4];

level.unshift(value);

if (level.length === 1) {
prev.unshift(level);
} else {
prev[0] = level;
}

return prev;
}, [] as string[][]);

result += levels.reduce((prev, item, index) => {
let _level = bigUnits[levels.length - index - 1];
let _item = item.join('').replace(/()\1+/g, '$1');

if (_item === '零') {
_level = '';
_item = '';
} else if (_item.endsWith('零')) {
_item = _item.slice(0, _item.length - 1);
}

return prev + _item + _level;
}, '');
} else {
result += '零';
}

result += '元';

// 小数部分
if (decimalPart !== '00') {
if (result === '零元') result = '';

for (let i = 0; i < decimalPart.length; i++) {
const digit = parseInt(decimalPart[i]);

if (digit !== 0) {
result += digits[digit] + decimalUnits[i];
}
}
} else {
result += '整';
}

return result;
} catch (error: any) {
return error.message;
}
};

export default capitalizedAmount;
1 change: 0 additions & 1 deletion src/coordinatesInCircle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function coordinatesInCircle(

let pointX: number, pointY: number;

// eslint-disable-next-line no-constant-condition
while (true) {
// 生成大于等于 xValueRange[0],小于等于 xValueRange[1] 的 x 坐标
pointX =
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ export { default as minBy } from './minBy';
export { default as maxBy } from './maxBy';

export { default as completeIp } from './completeIp';

export { default as capitalizedAmount } from './capitalizedAmount';

export { default as dataDesensitization } from './dataDesensitization';
export { default as BothLinkedList } from './BothLinkedList';
7 changes: 7 additions & 0 deletions test/math.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
factorial,
fibonacci,
sum,
capitalizedAmount,
} from '../src/index';
describe('数学计算相关测试', () => {
test('计算数组平均值', () => {
Expand Down Expand Up @@ -66,4 +67,10 @@ describe('数学计算相关测试', () => {
expect(fibonacci(4)).toBe(3);
expect(fibonacci(10)).toBe(55);
});

test('金额转换', () => {
expect(capitalizedAmount(100000000)).toBe('壹亿元整');
expect(capitalizedAmount('2023.04')).toBe('贰仟零贰拾叁元肆分');
expect(capitalizedAmount(-1024)).toBe('欠壹仟零贰拾肆元整');
});
});

0 comments on commit 45096a7

Please sign in to comment.