Commit e4cf6a29 authored by jiangweijia's avatar jiangweijia

更改

parent 69b65a64
const digits = [ const digits = ['壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '零']
'壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '零' const units = ['拾', '佰', '仟', '万', '亿', '兆']
]; const units2 = ['角', '分', '厘', '毫', '丝']
const units = [ const maxlength = 16
'拾', '佰', '仟', '万', '亿', '兆', function isDefined(value) {
]; return typeof value !== 'undefined'
const units2 = [ }
'角', '分', '厘', '毫', '丝' function isEmpty(val) {
]; // null or undefined
if (val == null) return true
export default function numberToWords(input: string|number): string {
input=String(input) if (typeof val === 'boolean') return false
let newInput
if (input === undefined || input === null || input === '' || !/^[\-]?\d{0,21}(\.\d{0,3})?$/.test(input)) { if (val === 0) return false
return input;
if (typeof val === 'number') return !val
if (val instanceof Error) return val.message === ''
switch (Object.prototype.toString.call(val)) {
// String or Array
case '[object String]':
case '[object Array]':
return !val.length
// Map or Set or File
case '[object File]':
case '[object Map]':
case '[object Set]': {
return !val.size
}
// Plain Object
case '[object Object]': {
return !Object.keys(val).length
}
} }
if(/[\-]\d{0,21}(\.\d{0,3})?$/.test(input)){
newInput = input.replace(/-/,""); return false
}else{ }
newInput = input; function toStdAmount(sAmount) {
if (!isDefined(sAmount) || isEmpty(sAmount)) return
// if (sAmount.indexOf('-') >= 0) {
// 替换掉负号
var rFlag = sAmount.indexOf('-') >= 0
sAmount = sAmount.replace(/-/g, '')
// }
var sComma = /\,/gi
var sResult = sAmount.replace(sComma, '')
var iDotIndex = sResult.indexOf('.')
var iLength = sResult.length
var toMatchNaNum = /\D/
var flag = true
if (
(iDotIndex != -1 &&
(iLength - iDotIndex > 3 || toMatchNaNum.test(sResult.slice(0, iDotIndex)))) ||
toMatchNaNum.test(sResult.slice(iDotIndex + 1, iLength))
) {
flag = false
return sResult.substr(0, iDotIndex + 3)
// 小数点后大于2位数 或 含有非数字字符
} else {
// 将金额处理为######.##形式
if (iDotIndex == -1) {
sResult = sResult + '.00'
} else if (iDotIndex == 0) {
if (iLength - iDotIndex == 1) sResult = '0' + sResult + '00'
if (iLength - iDotIndex == 2) sResult = '0' + sResult + '0'
if (iLength - iDotIndex == 3) sResult = '0' + sResult
} else {
if (iLength - iDotIndex == 2) sResult = sResult + '0'
if (iLength - iDotIndex == 1) sResult = sResult + '00'
}
// 处理金额非前面的0
var sTemp = ''
sTemp = sResult.slice(0, iDotIndex)
var iTemp = new Number(sTemp)
sTemp = iTemp.toString()
if (sTemp.length > maxlength) {
flag = false
return null
// 太长的金额
}
iDotIndex = sResult.indexOf('.')
sResult = sTemp + sResult.slice(iDotIndex)
// 返回######.##形式的金额
return rFlag ? '-' + sResult : sResult
} }
newInput = newInput.replace(/^0+$/, "0").replace(/^0+([1-9].*)/, "$1"); }
const CN1 = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; export default function numberToWords(sAmount: string | number): string | boolean {
const CN2 = ['元', '万', '亿','兆','京','垓']; sAmount = sAmount + ''
const CN3 = ['', '拾', '佰', '仟']; var rFlag = sAmount.indexOf('-') >= 0
const CN4 = ['角', '分', '厘']; sAmount = sAmount.replace(/-/g, '').replace(/[^\d\.\-]+/g, '')
newInput = newInput + ""; var value = toStdAmount(sAmount)
const arr = newInput.split("."); if (value.indexOf('.') != value.lastIndexOf('.')) return '金额输入有误'
let int = arr[0], dot = arr[1];//分成整数和小数两个部分 var sCN_Num = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖')
let intArr = int.split(""); var unit = new Array('元', '万', '亿', '万')
let counter = 0; var subunit = new Array('拾', '佰', '仟')
for (let i = intArr.length - 1; i >= 0; i--) { var sCNzero = '零'
intArr[i] = CN1[intArr[i]] + CN3[counter % 4]; var result = ''
if (counter % 4 == 0) { var iDotIndex = value.indexOf('.')
intArr[i] += CN2[counter / 4]; var sBeforeDot = value.slice(0, iDotIndex)
var sAfterDot = value.slice(iDotIndex)
var len = 0
len = sBeforeDot.length
var i = 0,
j = 0,
k = 0
// j is use to subunit,k is use to unit
var oldC = '3'
var cc = '0'
result = unit[0] + result
var oldHasN = false
var hasN = false
var allZero = true
for (var i = 0; i < len; i++) {
if (j == 0 && i != 0) {
if (!hasN) {
if (k % 2 == 0) result = result.slice(1)
} else {
if (oldC == '0') result = sCNzero + result
} }
counter++; if (oldC == '0' && result[0] !== '零' && result[0] !== '元') {
} result = '零' + result
int = intArr.join("").replace(/(零仟|零佰|零拾)/g, "零").replace(/零+/g, "零").replace(/零(元|万|亿|兆|京|垓)/g, "$1").replace(/亿万/, "亿").replace(/兆亿/,"兆").replace(/京兆/,"京").replace(/垓京/,"垓"); }
if (int === '元') { result = unit[k] + result
int = '零元'; oldHasN = hasN
hasN = false
}
cc = sBeforeDot.charAt(len - i - 1)
if (oldC == '0' && cc != oldC) {
if (hasN) result = sCNzero + result
}
if (cc != '0') {
if (j != 0) {
result = subunit[j - 1] + result
}
var dig = '0'
dig = sCN_Num[cc]
if (dig == '0') {
return false
}
hasN = true
allZero = false
result = dig + result
}
oldC = cc
j++
if (j == 4) {
k++
j = 0
}
} }
if (!dot || /^0+$/.test(dot)) { if (allZero) {
if(/^[\-]\d{0,21}(\.\d{0,3})?$/.test(input)){ result = '零元'
let value = input.length==1?"负":"负"+int+"整" } else {
return value; var bb = 0
}else{ if (!hasN) {
return int + "整"; bb++
if (!oldHasN) {
bb++
} }
}
if (bb != 0) {
result = result.slice(bb)
}
if (result.charAt(0) == '零') {
result = result.slice(1)
}
}
// after dot
sAfterDot = sAfterDot.slice(1)
len = sAfterDot.length
var corn = new Array('0', '0')
var cornunit = new Array('角', '分')
var n = 0
// j is use to subunit,k is use to unit
var dig = '0'
corn[0] = sAfterDot.charAt(0)
if (len > 1) {
corn[1] = sAfterDot.charAt(1)
} else {
corn[1] = '0'
}
if (corn[0] == '0' && corn[1] == '0') {
return rFlag ? (result = '-' + result + '整') : (result += '整')
} else { } else {
dot = dot.split("").map(function (item, index) { if (allZero) result = ''
return CN1[item] + CN4[index]; }
}).join(""); for (var i = 0; i < 2; i++) {
dot = dot.replace(/(零角|零分|零厘)/g, "零").replace(/零+/g, "零").replace(/零$/, ""); var curchar = corn[i]
if(/^[\-]\d{0,21}(\.\d{0,3})?$/.test(input)){ dig = sCN_Num[curchar]
return "负" + int + dot; if (i == 0) {
}else{ if (result != '' || curchar != '0') {
return int + dot; result += dig
}
if (curchar != '0') {
result += cornunit[0]
} }
}
if (i == 1 && curchar != '0') {
result = result + dig + cornunit[1]
}
} }
} return rFlag ? '负 ' + result : result
\ No newline at end of file }
...@@ -2,6 +2,13 @@ import { describe, it, expect } from 'vitest' ...@@ -2,6 +2,13 @@ import { describe, it, expect } from 'vitest'
import numberToWords from '../../src/money/numberToWords' import numberToWords from '../../src/money/numberToWords'
describe('测试整数', () => { describe('测试整数', () => {
it('17916.67 toBe 壹万柒仟玖佰壹拾陆元陆角柒分', () => {
expect(numberToWords(17916.67)).toBe('壹万柒仟玖佰壹拾陆元陆角柒分')
})
it('12500.06 toBe 壹万贰仟伍佰元零陆分', () => {
expect(numberToWords(12500.06)).toBe('壹万贰仟伍佰元零陆分')
})
it('1 toBe 壹元整', () => { it('1 toBe 壹元整', () => {
expect(numberToWords(1)).toBe('壹元整') expect(numberToWords(1)).toBe('壹元整')
}) })
......
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