Vue中阿拉伯数字与汉字的相互转换
用法:numberfilter (num) {const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] // changeNum[0] = "零"const unit = ['', '十', '百']num = parseInt(num)console.log(num);const getWan = (temp) =&
·
阿拉伯数字转汉字:
numberfilter (num) {
const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] // changeNum[0] = "零"
const unit = ['', '十', '百']
num = parseInt(num)
console.log(num);
const getWan = (temp) => {
const strArr = temp.toString().split('').reverse()
console.log(strArr);
let newNum = ''
for (var i = 0; i < strArr.length; i++) {
newNum = (i == 0 && strArr[i] == 0 ? '' : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? '' : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum
}
return newNum
}
const overWan = Math.floor(num / 100)
console.log(overWan);
let noWan = num % 100
console.log(noWan);
if (noWan.toString().length < 2) noWan = '0' + noWan
let strr = (overWan ? getWan(overWan) + '百' + getWan(noWan) : getWan(num))
console.log(strr.split('')[0]);
if(strr.split('')[0] == '一'){
return strr.substring(1)
}else{
return overWan ? getWan(overWan) + '百' + getWan(noWan) : getWan(num)
}
},
可以搭配filter使用:
汉字转阿拉伯数字
data:
chnNumChar : {
零:0,
一:1,
二:2,
三:3,
四:4,
五:5,
六:6,
七:7,
八:8,
九:9
},
chnNameValue : {
十:{value:10, secUnit:false},
百:{value:100, secUnit:false},
千:{value:1000, secUnit:false},
万:{value:10000, secUnit:true},
亿:{value:100000000, secUnit:true}
}
methods:
ChineseToNumber(chnStr) {
var rtn = 0;
var section = 0;
var number = 0;
var secUnit = false;
var str = chnStr.split("");
for (var i = 0; i < str.length; i++) {
var num = this.chnNumChar[str[i]];
if (typeof num !== "undefined") {
number = num;
if (i === str.length - 1) {
section += number;
}
} else {
var unit = this.chnNameValue[str[i]].value;
secUnit = this.chnNameValue[str[i]].secUnit;
if (secUnit) {
section = (section + number) * unit;
rtn += section;
section = 0;
} else {
section += number * unit;
}
number = 0;
}
}
return rtn + section;
},
更多推荐
已为社区贡献5条内容
所有评论(0)