代码改变世界

C#实现小写金额转换大写的方法

2012-10-25 22:34  朱峰(Peter.zhu)  阅读(378)  评论(0编辑  收藏  举报

 

C#实现小写金额转换大写的方法

 

 

  

        privatestring ConvertToCh(decimal vpdMoney, refstring vpsCnMoney)
        {
            string sCnDigit = "零壹贰叁肆伍陆柒捌玖"//0-9所对应的汉字 
            string sCnUnit = "万仟佰拾亿仟佰拾万仟佰拾元角分"//数字位所对应的汉字 
            string sTemp1 = "", sTemp2 = "";
            int i, j;
            string sCn1 = "";//数字的汉语读法 
            string sCn2 = "";//数字位的汉字读法 
            int iTemp, iZero = 0;//用来计算连续的零值是几个 
            
//将要转换的金额取绝对值并四舍五入取2位小数 
            vpdMoney = Math.Round(Math.Abs(vpdMoney), 2);
            //将要转换的金额乘100并转换成字符串形式
            sTemp2 = ((long)(vpdMoney * 100)).ToString();
            //判断是否溢出 
            j = sTemp2.Length;
            if (j > 15) { 
                return "溢出"
            }

            //取出对应位数的汉字位置。如:199.01,j为5所以:佰拾元角分
            sCnUnit = sCnUnit.Substring(15 - j);
            //循环取出每一位转换成相应的值 
            for (i = 0; i < j; i++)
            {
                sTemp1 = sTemp2.Substring(i, 1);
                //取出需转换的某一位的值 
                iTemp = Convert.ToInt32(sTemp1);
                //转换为数字 
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //当所取位数不为元、万、亿、万亿上的数字时 
                    if (sTemp1 == "0")
                    { sCn1 = ""; sCn2 = ""; iZero = iZero + 1; }
                    else
                    {
                        if (sTemp1 != "0" && iZero != 0)
                        {
                            sCn1 = "" + sCnDigit.Substring(iTemp * 11);
                            sCn2 = sCnUnit.Substring(i, 1);
                            iZero = 0;
                        }
                        else
                        {
                            sCn1 = sCnDigit.Substring(iTemp * 11);
                            sCn2 = sCnUnit.Substring(i, 1);
                            iZero = 0;
                        }
                    }
                }
                else
                {
                    //该位是万亿,亿,万,元位等关键位 
                    if (sTemp1 != "0" && iZero != 0)
                    {
                        sCn1 = "" + sCnDigit.Substring(iTemp * 11);
                        sCn2 = sCnUnit.Substring(i, 1); iZero = 0;
                    }
                    else
                    {
                        if (sTemp1 != "0" && iZero == 0)
                        {
                            sCn1 = sCnDigit.Substring(iTemp * 11);
                            sCn2 = sCnUnit.Substring(i, 1); iZero = 0;
                        }
                        else
                        {
                            if (sTemp1 == "0" && iZero >= 3)
                            {
                                sCn1 = ""; sCn2 = ""; iZero++;
                            }
                            else
                            {
                                if (j >= 11) { sCn1 = ""; iZero++; }
                                else { sCn1 = ""; sCn2 = sCnUnit.Substring(i, 1); iZero++; }
                            }
                        }
                    }
                }
                if (i == (j - 11) || i == (j - 3))
                {
                    //如果该位是亿位或元位,则必须写上 
                    sCn2 = sCnUnit.Substring(i, 1);
                }
                vpsCnMoney = vpsCnMoney + sCn1 + sCn2;
                if (i == j - 1 && sTemp1 == "0")
                {
                    //最后一位(分)为0时,加上“整” 
                    vpsCnMoney = vpsCnMoney + '';
                }
            }
            if (vpdMoney == 0) { vpsCnMoney = "零元整"; }  

            return"";

        }