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

ContractedBlock.gif ExpandedBlockStart.gif 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;
    }

转载于:https://www.cnblogs.com/200831856/archive/2009/07/19/ChinaToNum.html

猜你喜欢

转载自blog.csdn.net/weixin_34226182/article/details/93711318