c#货币金额大写转换方法

public static string CmycurD(decimal num)
        {
            string str1 = "零壹贰叁肆伍陆柒捌玖";            //0-9所对应的汉字
            string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
            string str3 = "";    //从原num值中取出的值
            string str4 = "";    //数字的字符串形式
            string str5 = "";  //人民币大写金额形式
            int i;    //循环变量
            int j;    //num的值乘以100的字符串长度
            string ch1 = "";    //数字的汉语读法
            string ch2 = "";    //数字位的汉字读法
            int nzero = 0;  //用来计算连续的零值是几个
            int temp;            //从原num值中取出的值
        
            num = Math.Round(Math.Abs(num),2);    //将num取绝对值并四舍五入取2位小数
            str4 = ((long)(num*100)).ToString();        //将num乘100并转换成字符串形式
            j = str4.Length;      //找出最高位
            if (j > 15){return "溢出";}
            str2 = str2.Substring(15-j);   //取出对应位数的str2的值。如:200.55,j为5所以tr2=佰拾元角分
              
            //循环取出每一位需要转换的值
            for(i=0;i            {
                str3 = str4.Substring(i,1);          //取出需转换的某一位的值
                temp = Convert.ToInt32(str3);      //转换为数字
                if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15))
                {    
                    //当所取位数不为元、万、亿、万亿上的数字时
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if(str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + str1.Substring(temp*1,1);
                            ch2 = str2.Substring(i,1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp*1,1);
                            ch2 = str2.Substring(i,1);
                            nzero = 0;
                        }
                    }
                }
                else
                {  
                    //该位是万亿,亿,万,元位等关键位
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "零" + str1.Substring(temp*1,1);
                        ch2 = str2.Substring(i,1);
                        nzero = 0;
                    }
                    else
                    {
                        if (str3 != "0" && nzero == 0)
                        {
                            ch1 = str1.Substring(temp*1,1);
                            ch2 = str2.Substring(i,1);
                            nzero = 0;
                        }
                        else
                        {
                            if (str3 == "0" && nzero >= 3)
                            {
                                ch1 = "";
                                ch2 = "";
                                nzero = nzero + 1;
                            }
                            else
                            {
                                if (j >= 11)
                                {
                                    ch1 = "";
                                    nzero = nzero + 1;
                                }
                                else
                                {
                                    ch1 = "";
                                    ch2 = str2.Substring(i,1);
                                    nzero = nzero + 1;
                                }
                            }
                        }
                    }
                }
                if (i == (j-11) || i == (j-3))
                {  
                    //如果该位是亿位或元位,则必须写上
                    ch2 = str2.Substring(i,1);
                }
                str5 = str5 + ch1 + ch2;
    
                if (i == j-1 && str3 == "0" )
                {    
                    //最后一位(分)为0时,加上“整”
                    str5 = str5 + '整';
                }
            }
            if (num == 0)
            {
                str5 = "零元整";
            }
            return str5;
        }


二、

 程序代码
// 货币金额大写转换库   Money2String.dll

// 静态中文转换方法     String GetCnString(String MoneyString)
// 最大转化金额         9999999999999999.99  (16位整数位,2位小数位)
// 方法返回值           转换成功则返回非零长字串,否则返回零长字串

// 静态英文转换方法     String GetEnString(String MoneyString)
// 最大转化金额         999999999999999.99   (15位整数位,2位小数位)
// 方法返回值           转换成功则返回非零长字串,否则返回零长字串


// 程序设计 xlFancy     版本 2003.03.18.22 - 2003.04.09
// 邮箱地址 [email protected]

using System;

namespace Money2String
{
    public class Money2String
    {

        private static readonly String   cnNumber      = "零壹贰叁肆伍陆柒捌玖";
        private static readonly String   cnUnit        = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";

        private static readonly String[] enSmallNumber = {"","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
        private static readonly String[] enLargeNumber = {"TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
        private static readonly String[] enUnit        = {"","THOUSAND","MILLION","BILLION","TRILLION"};


        // 以下是货币金额中文大写转换方法
        public static String GetCnString(String MoneyString)
        {
            String [] tmpString  = MoneyString.Split('.');
            String    intString  = MoneyString;   // 默认为整数
            String    decString  = "";            // 保存小数部分字串
            String    rmbCapital = "";            // 保存中文大写字串
            int       k;
            int       j;
            int       n;

            if (tmpString.Length>1)
            {
                intString = tmpString[0];             // 取整数部分
                decString = tmpString[1];             // 取小数部分
            }
            decString += "00";
            decString  = decString.Substring (0,2);   // 保留两位小数位
            intString += decString;

            try
            {
                k = intString.Length -1;
                if (k>0 && k<18)
                {
                    for (int i=0;i<=k;i++)
                    {
                        j=(int)intString[i]-48;
                        // rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];     // 供调试用的直接转换
                        n = i+1 >= k ? (int)intString[k]-48:(int)intString[i+1]-48; // 等效于 if( ){ }else{ }
                        if (j==0)
                        {
                            if(k-i==2 || k-i==6 || k-i==10 || k-i==14)
                            {
                                rmbCapital += cnUnit[k-i];
                            }
                            else
                            {
                                if (n != 0)
                                {
                                    rmbCapital  += cnNumber[j];
                                }
                            }
                        }
                        else
                        {
                            rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];
                        }
                    }

                    rmbCapital = rmbCapital.Replace ("兆亿万","兆");
                    rmbCapital = rmbCapital.Replace ("兆亿","兆");
                    rmbCapital = rmbCapital.Replace ("亿万","亿");
                    rmbCapital = rmbCapital.TrimStart('元');
                    rmbCapital = rmbCapital.TrimStart('零');

                    return rmbCapital;
                }
                else
                {
                    return "";   // 超出转换范围时,返回零长字串
                }
            }
            catch
            {
                return "";   // 含有非数值字符时,返回零长字串
            }
        }


        // 以下是货币金额英文大写转换方法
        public static String GetEnString(String MoneyString)
        {
            String [] tmpString  = MoneyString.Split('.');
            String    intString  = MoneyString;   // 默认为整数
            String    decString  = "";            // 保存小数部分字串
            String    engCapital = "";            // 保存英文大写字串
            String    strBuff1;
            String    strBuff2;
            String    strBuff3;
            int       curPoint;
            int       i1;
            int       i2;
            int       i3;
            int       k;
            int       n;
    
            if (tmpString.Length >1)
            {
                intString = tmpString[0];             // 取整数部分
                decString = tmpString[1];             // 取小数部分
            }
            decString += "00";
            decString  = decString.Substring (0,2);   // 保留两位小数位
    
            try
            {
                // 以下处理整数部分
                curPoint = intString.Length-1;
                if (curPoint>=0 && curPoint<15)
                {
                    k = 0;
                    while(curPoint>=0)
                    {
                        strBuff1 = "";
                        strBuff2 = "";
                        strBuff3 = "";
                        if (curPoint>=2)
                        {
                            n=int.Parse(intString.Substring(curPoint-2,3));
                            if (n !=0)
                            {
                                i1 = n/100;            // 取佰位数值
                                i2 = (n-i1*100)/10;    // 取拾位数值
                                i3 = n-i1*100-i2*10;   // 取个位数值
                                if (i1 !=0)
                                {
                                    strBuff1 = enSmallNumber[i1] + " HUNDRED ";
                                }
                                if (i2!=0)
                                {
                                    if (i2==1)
                                    {
                                        strBuff2 = enSmallNumber[i2*10+i3] + " ";
                                    }
                                    else
                                    {
                                        strBuff2 = enLargeNumber[i2-2] + " ";
                                        if (i3 !=0)
                                        {
                                            strBuff3 = enSmallNumber[i3] + " ";
                                        }
                                    }
                                }
                                else
                                {
                                    if (i3 !=0)
                                    {
                                        strBuff3 = enSmallNumber[i3] + " ";
                                    }
                                }
                                engCapital = strBuff1 + strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;    
                            }
                        }
                        else
                        {
                            n = int.Parse(intString.Substring(0,curPoint+1));
                            if (n !=0)
                            {
                                i2 = n/10;      // 取拾位数值
                                i3 = n-i2*10;   // 取个位数值
                                if (i2!=0)
                                {
                                    if (i2==1)
                                    {
                                        strBuff2 = enSmallNumber[i2*10+i3] + " ";
                                    }
                                    else
                                    {
                                        strBuff2 = enLargeNumber[i2-2] + " ";
                                        if (i3 !=0)
                                        {
                                            strBuff3 = enSmallNumber[i3] + " ";
                                        }
                                    }
                                }
                                else
                                {
                                    if (i3 !=0)
                                    {
                                        strBuff3 = enSmallNumber[i3] + " ";
                                    }
                                }
                                engCapital = strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;
                            }
                        }
            
                        ++k;
                        curPoint -= 3;
                    }
                    engCapital = engCapital.TrimEnd();
                }

    
                // 以下处理小数部分
                strBuff2 = "";
                strBuff3 = "";
                n = int.Parse(decString);
                if (n !=0)
                {
                    i2 = n/10;      // 取拾位数值
                    i3 = n-i2*10;   // 取个位数值
                    if (i2!=0)
                    {
                        if (i2==1)
                        {
                            strBuff2 = enSmallNumber[i2*10+i3] + " ";
                        }
                        else
                        {
                            strBuff2 = enLargeNumber[i2-2] + " ";
                            if (i3 !=0)
                            {
                                strBuff3 = enSmallNumber[i3] + " ";
                            }
                        }
                    }
                    else
                    {
                        if (i3 !=0)
                        {
                            strBuff3 = enSmallNumber[i3] + " ";
                        }
                    }

                    // 将小数字串追加到整数字串后
                    if (engCapital.Length>0)
                    {
                        engCapital = engCapital + " AND CENTS " + strBuff2+strBuff3;   // 有整数部分时
                    }
                    else
                    {
                        engCapital = "CENTS " + strBuff2 + strBuff3;   // 只有小数部分时
                    }
                }    

                engCapital = engCapital.TrimEnd();
                return engCapital;
            }
            catch
            {
                return "";   // 含非数字字符时,返回零长字串
            }
        }
    }
}

你可能感兴趣的:(C#开发,c#,string,class)