码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-06-27 16:14:31      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:har   variable   ===   func   汉字   string   阿拉伯   word   rip   

/**
* 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

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

标签:har   variable   ===   func   汉字   string   阿拉伯   word   rip   

原文地址:https://www.cnblogs.com/sichuanren/p/11097528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!