C#版中文数字转阿拉伯数字

        原本是群里某位大哥发的JavaScript版,因为项目里需要,改写成了C#版的,希望能对需要的人帮点忙哈~~~

 1          /**/ /// <summary>
 2        /// 将中文数字替换为阿拉伯数字
 3        /// </summary>
 4        /// <param name="word"></param>
 5        /// <returns></returns>

 6          public   static   string  WordToNumber( string  word)
 7          {
 8            string e = "([零一二三四五六七八九十百千万亿])+";
 9            MatchCollection mc = Regex.Matches(word, e);
10
11            foreach(Match m in mc)
12            {
13                word = word.Replace(m.Value, Word2Number(m.Value));
14            }

15            return word;
16        }

17
18          private   static   string  Word2Number( string  w)
19          {
20            if(w == "")
21                return w;
22
23            string e = "零一二三四五六七八九";
24            string[] ew = new string[]{""""""};
25            string ewJoin = "十百千";
26            string[] ej = new string[]{"""亿"};
27
28            string rss = "^([" + e + ewJoin + "]+" + ej[1+ ")?([" + e
29                + ewJoin + "]+" +ej[0+ ")?([" + e + ewJoin + "]+)?$";
30            string[] mcollect = Regex.Split(w, rss);
31            if(mcollect.Length < 4)
32                return w;
33            return (
34                Convert.ToInt64(foh(mcollect[1])) * 100000000 +
35                Convert.ToInt64(foh(mcollect[2])) * 10000 +
36                Convert.ToInt64(foh(mcollect[3]))
37                ).ToString();
38        }

39
40          private   static   int  foh( string  str)
41          {
42            string e = "零一二三四五六七八九";
43            string[] ew = new string[]{""""""};
44            string[] ej = new string[]{"""亿"};
45
46            int a = 0;
47            if(str.IndexOf(ew[0]) == 0)
48                a = 10;
49            str = Regex.Replace(str, e[0].ToString(), "");
50
51            if(Regex.IsMatch(str, "([" + e + "])$"))
52            {
53                a += e.IndexOf(Regex.Match(str, "([" + e + "])$").Value[0]);
54            }

55
56            if(Regex.IsMatch(str, "([" + e + "])" + ew[0]))
57            {
58                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[0]).Value[0]) * 10;
59            }

60
61            if(Regex.IsMatch(str, "([" + e + "])" + ew[1]))
62            {
63                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[1]).Value[0]) * 100;
64            }

65
66            if(Regex.IsMatch(str, "([" + e + "])" + ew[2]))
67            {
68                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[2]).Value[0]) * 1000;
69            }

70            return a;
71        }

转载于:https://www.cnblogs.com/Alacky/archive/2007/09/20/900520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值