1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > vue 小写金额转换为大写金额

vue 小写金额转换为大写金额

时间:2022-04-13 17:20:27

相关推荐

vue 小写金额转换为大写金额

vue中小写金额转换为大写金额

<div class="total"><span style="width:10%;display:inline-block; margin-left:10%">总金额:</span><span style="width:40%;display:inline-block">大写:{{smallToBig(this.moneyTotal)}}</span><span style="width:40%;display:inline-block">小写:{{moneyTotal}} 元</span></div>

data() {return {,moneyTotal: 1234,}},methods: {smallToBig(money = this.moneyTotal) {// 汉字的数字const cnNums = ['零', '壹','贰','叁','肆','伍', '陆','柒','捌','玖',]// 基本单位const cnIntRadice = ['', '拾', '佰', '仟']// 对应整数部分扩展单位const cnIntUnits = ['', '万', '亿', '兆']// 对应小数部分单位const cnDecUnits = ['角', '分']// 整数金额时后面跟的字符const cnInteger = '整'// 整型完以后的单位const cnIntLast = '元'// 最大处理的数字const maxNum = 9999999999999999.99// 金额整数部分let integerNum// 金额小数部分let decimalNum// 输出的中文金额字符串let chineseStr = ''// 分离金额后用的数组,预定义let partsif (money === '') {return ''}money = parseFloat(money)if (money >= maxNum) {// 超出最大处理数字return ''}if (money === 0) {chineseStr = cnNums[0] + cnIntLast + cnIntegerreturn chineseStr}// 转换为字符串money = money.toString()if (money.indexOf('.') === -1) {integerNum = moneydecimalNum = ''} else {parts = money.split('.')integerNum = parts[0]decimalNum = parts[1].substr(0, 4)}// 获取整型部分转换if (parseInt(integerNum, 10) > 0) {let zeroCount = 0const IntLen = integerNum.lengthfor (let i = 0; i < IntLen; i++) {const n = integerNum.substr(i, 1)const p = IntLen - i - 1const q = p / 4const m = p % 4if (n === '0') {zeroCount++} else {if (zeroCount > 0) {chineseStr += cnNums[0]}// 归零zeroCount = 0//alert(cnNums[parseInt(n)])chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]}if (m === 0 && zeroCount < 4) {chineseStr += cnIntUnits[q]}}chineseStr += cnIntLast}// 小数部分if (decimalNum !== '') {const decLen = decimalNum.lengthfor (let i = 0; i < decLen; i++) {const n = decimalNum.substr(i, 1)if (n !== '0') {chineseStr += cnNums[Number(n)] + cnDecUnits[i]}}}if (chineseStr === '') {chineseStr += cnNums[0] + cnIntLast + cnInteger} else if (decimalNum === '') {chineseStr += cnInteger}return chineseStr},}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。