javascript小应用之汉字数字转阿拉伯数字

阅读更多
汉字 转 数字


/**
*  word2number
*/

function word2number(w){
    var e = "零一二三四五六七八九";
    var ew = ["十","百","千"];
    var ej = ["万","亿"];
    var rss = "^(["+e+ew.join("")+"]+"+ej[1]+")?(["+e+ew.join("")+"]+"+ej[0]+")?(["+e+ew.join("")+"]+)?$";
    //     ^([零一二三四五六七八九十百千]+亿)?([零一二三四五六七八九十百千]+万)?([零一二三四五六七八九十百千]+)?$ 
    var arr = new RegExp(rss).exec(w);
    function foh(str){
        str = new String(str);    
        var a=0;
        if(str.indexOf(ew[0])==0)a=10;              
            str=str.replace(new RegExp(e.charAt(0),"g"),"");
        if(new RegExp("(["+e+"])$").test(str))
            a+=e.indexOf(RegExp.$1);
        if(new RegExp("(["+e+"])"+ew[0]).test(str))
            a+=e.indexOf(RegExp.$1)*10;
        if(new RegExp("(["+e+"])"+ew[1]).test(str))
            a+=e.indexOf(RegExp.$1)*100;
        if(new RegExp("(["+e+"])"+ew[2]).test(str))
            a+=e.indexOf(RegExp.$1)*1000;
        return a;
    }
    return foh(arr[1])*100000000+foh(arr[2])*10000+foh(arr[3]);
}

//Test
//==============

var str1 = "二十五";
var str2 = "五百零二";
var str3 = "四千五百";
var str4 = "七十亿零八百五十万四千五百零二";

console.log(str1, " : ", word2number(str1));
console.log(str2, " : ", word2number(str2));
console.log(str3, " : ", word2number(str3));
console.log(str4, " : ", word2number(str4));



//end










-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2009406




















-

你可能感兴趣的:(js,javascript,汉字,转,数字)