// 汉字数字转换为数字 //str2还没有成功 const numList = ['零','一','二','三','四','五','六','七','***']; const strList = ['十','百','千','万','亿']; const change = function(str){ var qian = '',bai = '',shi = ''; if(str.includes('千')){ qian = str.slice(0,str.indexOf('千')); str = str.slice(str.indexOf('千')+1,str.length); } if(str.includes('百')){ bai = str.slice(0,str.indexOf('百')); str = str.slice(str.indexOf('百')+1,str.length); } if(str.includes('十')){ shi = str.slice(0,str.indexOf('十')); str = str.slice(str.indexOf('十')+1,str.length); } qian = Number(numList.indexOf(qian)); bai = Number(numList.indexOf(bai)); shi = Number(numList.indexOf(shi)); return qian*1000+bai*100+shi*10+Number(numList.indexOf(str)); } var str2number = function(str){ if(!str) return 0; str = str.split(''); var yi = '',wan = '',yiwan=''; if(str.includes('亿')){ yi = str.slice(0,str.indexOf('亿')); str = str.slice(str.indexOf('亿')+1,str.length); if(yi.includes('万')){ yiwan = yi.slice(0,yi.indexOf('万')); yi = yi.slice(yi.slice('万')+1,yi.length); } } if(str.includes('万')){ wan = str.slice(0,str.indexOf('万')); str = str.slice(str.indexOf('万')+1,str.length); } yiwan = change(yiwan.join(''))*10000*100000000; yi = change(yi.join(''))*100000000; return yiwan+yi+change(wan.join(''))*10000+change(str.join('')); } str = '一千九百三十一万五千四百三十二亿九千八百七十六万四千三百零二'; str1 = '九千八百七十六万四千三百零二'; str2 = '一千零一万五千四百三十二亿九千八百七十六万四千三百零二' console.log(str2number(str));
全部评论
(0) 回帖