欢迎大家来我的Bolg作客!在这里我们将为您提供及时全面的IT信息……

ASP.NET(C#)将汉字数字转换成阿拉伯数字

Code
    struct Number
    {
        
public int this[char c]
        {
            
get
            {
                
switch (c)
                {
                    
case'':return 1;
                    
case'':return 2;
                    
case'':return 3;
                    
case'':return 4;
                    
case'':return 5;
                    
case'':return 6;
                    
case'':return 7;
                    
case'':return 8;
                    
case'':return 9;
                    
case'':return 0;
                    
default:return -1;
                }
            }
        }
    }

/// <summary>
/// 
/// </summary>
    struct Unit
    {
        
public int this[char c]
        {
            
get
            {
                
switch (c)
                {
                    
case'':return 10;
                    
case'':return 100;
                    
case'':return 1000;
                    
case'':return 10000;
                    
case'亿':return 100000000;
                    
default:return 1;
                }
            }
        }
    }

    
public long Parse(string cnum)
    {
        Unit unit;
        Number number;
        cnum 
= Regex.Replace(cnum, "\\s+""");
        
long firstUnit = 1;//一级单位                
        long secondUnit = 1;//二级单位 
        long tmpUnit = 1;
        
long result = 0;
        
for (int i = cnum.Length - 1; i> -1;--i)
        {
            tmpUnit 
= unit[cnum[i]];
            
if (tmpUnit > firstUnit)
            {
                firstUnit 
= tmpUnit;
                secondUnit 
= 1;
                
continue;
            }
            
else if(tmpUnit > secondUnit)
            {
                secondUnit 
= tmpUnit;
                
continue;
            }
            result 
+= firstUnit * secondUnit * number[cnum[i]];
        }
        
return result;
    }
posted @ 2009-07-19 15:01  Cyrus Dai  阅读(1143)  评论(1编辑  收藏  举报
Copyright ? 戴超 2008-10-30---9595-95-95