阿拉伯数字金额转繁体金额算法

csharp

浏览数:1,020

2019-1-7


具体代码

using System;
using System.Text;

namespace 阿拉伯转繁体
{
    class Program
    {
        static void Main(string[] args)
        {
            
            long n = 12301310040;
            long c = 1;
            long a = n;
            StringBuilder sb = new StringBuilder();

            while (n > 0)
            {
                if (a < 10)
                {
                    sb.Append(FanTi((int)a));    //获取数字对应的繁体
                    sb.Append(DanWei(c));   //获取对应的位数
                    n = n - (a * c);    //用于去除第一位 
                    if (n < (c / 10) && n != 0)     //判断中间是否有零 并且判断不是最后一位为0
                    {
                        sb.Append(FanTi(0));
                    }
                    a = n;  //将a赋值为没有第一位的数
                    c = 1;
                }
                else
                {
                    a = a / 10;     //每次除10  用于拿到输入数字的第一位,当整除到a<10时就代表拿到了第一位
                    c *= 10;        //看看循环了多少次,每次循环就代表位数左移了一位   个十百千万
                }

            }
            sb.Append("圆");
            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }

        static string DanWei(long n)
        {
            switch (n)
            {
                case 1000000000000:
                    return "兆";
                case 100000000000:
                    return "仟";
                case 10000000000:
                    return "佰";
                case 1000000000:
                    return "拾";
                case 100000000:
                    return "亿";
                case 10000000:
                    return "仟";
                case 1000000:
                    return "佰";
                case 100000:
                    return "拾";
                case 10000:
                    return "万";
                case 1000:
                    return "仟";
                case 100:
                    return "佰";
                case 10:
                    return "拾";
            }            return "";
        }

        static string FanTi(int n)
        {
            string[] fanTi = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            return fanTi[n];
        }
    }
}