-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
af7ccd5
commit 6231140
Showing
6 changed files
with
200 additions
and
3 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
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
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,14 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { numberToChinese } from '../index' | ||
|
||
describe('numberToChinese', () => { | ||
it('should convert numbers to Chinese currency format', () => { | ||
expect(numberToChinese(123456.78)).toBe('壹拾贰万叁仟肆佰伍拾陆元柒角捌分') | ||
expect(numberToChinese(100000000)).toBe('壹亿元整') | ||
expect(numberToChinese(0)).toBe('零元整') | ||
}) | ||
|
||
it('should handle negative numbers', () => { | ||
expect(numberToChinese(-123456.78)).toBe('欠壹拾贰万叁仟肆佰伍拾陆元柒角捌分') | ||
}) | ||
}) |
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,41 @@ | ||
/** | ||
* 数字转化为大写金额 | ||
* @category Number | ||
* @param {number} num | ||
* @returns {string} | ||
* @example | ||
* ``` typescript | ||
* numberToChinese(100) | ||
* ``` | ||
*/ | ||
export const numberToChinese = (num: number): string => { | ||
const fraction = ['角', '分'] | ||
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] | ||
const unit = [ | ||
['元', '万', '亿'], | ||
['', '拾', '佰', '仟'] | ||
] | ||
const head = num < 0 ? '欠' : '' | ||
num = Math.abs(num) | ||
let s = '' | ||
for (let i = 0; i < fraction.length; i++) { | ||
s += (digit[Math.floor(num * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '') | ||
} | ||
s = s || '整' | ||
num = Math.floor(num) | ||
for (let i = 0; i < unit[0].length && num > 0; i++) { | ||
let p = '' | ||
for (let j = 0; j < unit[1].length && num > 0; j++) { | ||
p = digit[num % 10] + unit[1][j] + p | ||
num = Math.floor(num / 10) | ||
} | ||
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s | ||
} | ||
return ( | ||
head + | ||
s | ||
.replace(/(零.)*零元/, '元') | ||
.replace(/(零.)+/g, '零') | ||
.replace(/^整$/, '零元整') | ||
) | ||
} |
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
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