导航

金额转换.小To大

Posted on 2006-02-08 10:56  wngwz  阅读(383)  评论(0编辑  收藏  举报
namespace BaseSystemFramework
{
    
using System;
    
public class Capitalization
    
{
        
public Capitalization()
        
{
        }


        
//金额数字(小写)转换成文字(大写) 
        private static string[] shuzi={"","","","","","","","","",""};
        
private static string[] danwei={"","","","","","","","","","","亿","","",""};

        
/// <summary>
        
/// 金额小写转换成大写
        
/// </summary>
        
/// <param name="payment"></param>
        
/// <returns></returns>

        public static string MoneyConvert(double payment)
        
{
            
string money;
            
int ipayment, itemp, num, length, danweinum;
            
string spayment, stemp;
            
bool yuan, wan, yi, zero;

            yi 
= false;
            wan 
= false;
            yuan 
= false;
            zero 
= false;
            money 
= "";
            ipayment 
= (int)Math.Floor(payment*100+0.5);   //转换成分并四舍五入
            if( ipayment > 0 )
            
{
                spayment 
= ipayment.ToString();    //转换成字符串
                length = spayment.Length;
                
if( length > 0 )
                
{
                    money 
= "";
                    num 
= length - 1;
                    danweinum 
= 0;
                    
while( num >= 0 )
                    
{
                        stemp 
= spayment.Substring( num, 1 );
                        itemp 
= int.Parse( stemp );
                        
if( itemp > 0 )
                        
{
                            
if( money.Length > 0 )
                            
{
                                stemp 
= shuzi[itemp] + danwei[danweinum];
                                
if( danweinum == 10 )
                                    yi 
= true;
                                
if( danweinum == 6 )
                                    wan 
= true;
                                
if( danweinum == 2 )
                                    yuan 
= true;
                                
if( danweinum > 10 )
                                    
if!yi )
                                    
{
                                        stemp 
= stemp + "亿";
                                        yi 
= true;
                                    }

                                
if( danweinum > 6 )
                                    
if!wan )
                                    
{
                                        stemp 
= stemp + "";
                                        wan 
= true;
                                    }

                                
if( danweinum > 2 )
                                    
if!yuan )
                                    
{
                                        stemp 
= stemp + "";
                                        yuan 
= true;
                                    }

                                money 
= stemp + money;
                            }

                            
else
                            
{
                                
if( danweinum > 0 )
                                
{
                                    stemp 
= shuzi[itemp] + danwei[danweinum];
                                    
if( danweinum == 10 )
                                        yi 
= true;
                                    
if( danweinum == 6 )
                                        wan 
= true;
                                    
if( danweinum == 2 )
                                        yuan 
= true;
                                    
if( danweinum > 10 )
                                        
if!yi )
                                        
{
                                            stemp 
= stemp + "亿";
                                            yi 
= true;
                                        }

                                    
if( danweinum > 6 )
                                        
if!wan )
                                        
{
                                            stemp 
= stemp + "";
                                            wan 
= true;
                                        }

                                    
if( danweinum > 2 )
                                        
if!yuan )
                                        
{
                                            stemp 
= stemp + "";
                                            yuan 
= true;
                                        }

                                    money 
= stemp + "";
                                }

                                
else
                                    money 
= shuzi[itemp] + danwei[danweinum];
                            }

                            zero 
= false;
                        }

                        
else
                        
{
                            
if( money.Length > 0 )
                                
if!zero )
                                
{
                                    money 
= shuzi[itemp] + money;
                                }

                            zero 
= true;
                        }

                        num
--;
                        danweinum
++;
                    }

                }

            }

            
return money;
        }


        
/// <summary>
        
/// 金额小写转换成大写
        
/// </summary>
        
/// <param name="payment"></param>
        
/// <returns></returns>

        public static string MoneyConvert( string payment )
        
{
            
if( payment.Length != 0)
                
return MoneyConvert( Convert.ToDouble( payment ) );
            
else
                
return MoneyConvert( Convert.ToDouble( "0" ) );
        }

    }

}