Commit 1d7da811 authored by sunguoshu's avatar sunguoshu

fix

parent f008d11d
const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] as const const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] as const
const units = ['', '拾', '佰', '仟'] as const const units = ['', '拾', '佰', '仟'] as const
const units2 = ['', '万', '亿'] as const const units2 = ['', '万', '亿'] as const
const units3 = ['角', '分', '厘'] as const const units3 = ['角', '分'] as const
export default function numberToWords(input: string | number): string { export default function numberToWords(input: string | number): string {
const number = Number(input) let [integer = '', decimal = ''] = String(input).split('.')
if (number >= 10 ** 13) throw new Error('数字太大') decimal = decimal.slice(0, 2)
const number = Number(integer + '.' + decimal)
if (number >= 10 ** 12) throw new Error('数字太大')
if (number < 0) throw new Error('数字不能为负数') if (number < 0) throw new Error('数字不能为负数')
if (number === 0) return '零元整' if (number === 0) return '零元整'
let [integer = '', decimal = ''] = String(number).split('.') ;[integer = '', decimal = ''] = String(number).split('.')
integer = integer.padStart(12, '0').slice(-12) integer = integer.slice(-12)
decimal = decimal.padEnd(2, '0').slice(0, 2) decimal = decimal.slice(0, 2)
let result = '' let result = ''
if (integer) { if (integer) {
let preNull = true let preNull = true
...@@ -39,7 +41,7 @@ export default function numberToWords(input: string | number): string { ...@@ -39,7 +41,7 @@ export default function numberToWords(input: string | number): string {
} }
} }
if (result) result += '元' if (result) result += '元'
if (!decimal || decimal === '00') return result + '整' if (!decimal) return result + '整'
if (decimal) { if (decimal) {
for (let i = 0; i < decimal.length; i++) { for (let i = 0; i < decimal.length; i++) {
const digit = digits[Number(decimal[i])] const digit = digits[Number(decimal[i])]
......
import { describe, it, expect } from 'vitest' import { describe, it, expect } from 'vitest'
import numberToWords from '../../src/money/numberToWords' import numberToWords2 from '../../src/money/numberToWords'
import numberToWords2 from '../../src/money/numberToWords2' import numberToWords from '../../src/money/numberToWords2'
import numberToWords3 from '../../src/money/numberToWords3' import numberToWords3 from '../../src/money/numberToWords3'
describe('测试整数', () => { describe('测试整数', () => {
...@@ -229,6 +229,9 @@ describe('测试小数', () => { ...@@ -229,6 +229,9 @@ describe('测试小数', () => {
}) })
describe('测试极端小数', () => { describe('测试极端小数', () => {
it('0.001 toBe 零元整', () => {
expect(numberToWords(0.001)).toBe('零元整')
})
it('1.001 toBe 壹元整', () => { it('1.001 toBe 壹元整', () => {
expect(numberToWords(1.001)).toBe('壹元整') expect(numberToWords(1.001)).toBe('壹元整')
}) })
...@@ -259,23 +262,14 @@ describe('小数整数', () => { ...@@ -259,23 +262,14 @@ describe('小数整数', () => {
it('839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分', () => { it('839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分', () => {
expect(numberToWords(839109939.677061)).toBe('捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分') expect(numberToWords(839109939.677061)).toBe('捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分')
}) })
it('839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分', () => {
expect(numberToWords2(839109939.677061)).toBe('捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分')
})
it('839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分', () => {
expect(numberToWords3(839109939.677061)).toBe('捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分')
})
}) })
describe('测试特殊情况', () => { describe('测试特殊情况', () => {
it('-1 toBe error', () => { it('-1 toBe error', () => {
expect(numberToWords(-1)).toBe('负壹元整') expect(() => numberToWords(-1)).toThrowError()
})
it('-1 toBe error', () => {
expect(() => numberToWords2(-1)).toThrowError('数字不能为负数')
}) })
it('-1 toBe error', () => { it('1000000000000 toBe error', () => {
expect(numberToWords3(-1)).toBe('负壹元整') expect(() => numberToWords(1000000000000)).toThrowError()
}) })
}) })
...@@ -284,7 +278,7 @@ describe('随机数测试', () => { ...@@ -284,7 +278,7 @@ describe('随机数测试', () => {
for (let i = 0; i < 10000; i++) { for (let i = 0; i < 10000; i++) {
const random = Math.random() * 1000000000000 const random = Math.random() * 1000000000000
const result = numberToWords3(random) const result = numberToWords3(random)
const result2 = numberToWords2(random) const result2 = numberToWords(random)
expect(result).toBe(result2) expect(result).toBe(result2)
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment