/**
* 汉字转化为数字
* @param chnStr
* @returns {number}
* @constructor
*/
let chnNumChar = {
零:0,
一:1,
二:2,
三:3,
四:4,
五:5,
六:6,
七:7,
八:8,
九:9
};
let chnNameValue = {
十:{value:10, secUnit:false},
百:{value:100, secUnit:false},
千:{value:1000, secUnit:false},
万:{value:10000, secUnit:true},
亿:{value:100000000, secUnit:true}
}
function ChineseToNumber(chnStr){
let rtn =0;
let section =0;
let number =0;
let secUnit =false;
let str = chnStr.split('');
for(let i =0; i < str.length; i++){
let num = chnNumChar[str[i]];
if(typeof num !=='undefined'){
number = num;
if(i === str.length -1){
section += number;
}
}else{
let unit = chnNameValue[str[i]].value;
secUnit = chnNameValue[str[i]].secUnit;
if(secUnit){
section = (section + number) * unit;
rtn += section;
section =0;
}else{
section += (number * unit);
}
number =0;
}
}
return rtn + section;
}
/**
* 数字转化为汉字
* @param num
* @returns {string}
* @constructor
*/
function NumberToChinese(num){
let N = [
"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"
];
let str = num.toString();
let len = num.toString().length;
let C_Num = [];
for (let i =0; i < len; i++) {
C_Num.push(N[str.charAt(i)]);
}
return C_Num.join('');
}
网友评论