将数字转换为大写数字

       public static string[] units = { "", "拾", "佰", "仟" };```

       static void Main(string[] args)
       {
           Console.WriteLine("输入:");
           string a = Console.ReadLine();
           if (a.Length > 12)
           {
               Console.WriteLine("超过数字极限");
               Console.ReadLine();
               return;
           }
           Change(a);
           Console.ReadLine();
       }

       static void Change(string x)
       {


           //1011 1100 2400
           //壹仟零壹拾壹 亿 壹仟壹佰 万 零贰仟肆佰

           //万以内
           string small = "";
           //亿以内万以外
           string middle = "";
           //亿以外
           string large = "";
           if (x.Length <= 4)
               small = x;
           else if (x.Length <= 8 && x.Length > 4)
           {
               small = x.Substring(x.Length - 4, 4);
               middle = x.Substring(0, x.Length - 4);
           }
           else if (x.Length > 8)
           {
               small = x.Substring(x.Length - 4, 4);
               middle = x.Substring(x.Length - 8, 4);
               large = x.Substring(0, x.Length - 8);
           }

           small = Check0Change(small);
           middle = Check0Change(middle);
           large = Check0Change(large);
           string end = "";
           end = ChangeNumber(large, end, "亿");
           if (middle != "aaaa")
               end = ChangeNumber(middle, end, "万");
           end = ChangeNumber(small, end, "");
           Console.WriteLine($"end:{end}圆整");
       }

       private static string ChangeNumber(string small, string end, string danwei)
       {
           if (small != "")
           {
               for (int i = 0; i < small.Length; i++)
               {
                   if (small[i] != 'a')
                   {
                       uint y = uint.Parse((small[i]).ToString());
                       end += nums[y];
                   }
                   if (small[i] != 'a' && small[i] != '0')
                       end += units[small.Length - 1 - i];
               }
               end += danwei;
           }
           return end;
       }

       /// 
       /// 检测是否翻译0(4位数)
       /// 
       /// 翻译的话返回0;不翻译返回a
       static string Check0Change(string a)
       {
           char[] b = a.ToCharArray();

           for (int i = b.Length - 1; i >= 0; i--)
           {
               if (b[i] == '0')
               {
                   b[i] = 'a';
               }
               else
                   break;
           }

           for (int i = 0; i < b.Length - 1; i++)
           {
               if (b.Length > 1)
               {
                   Console.WriteLine($"first:{b[i]} last{b[i + 1]}");
                   if (b[i] == '0' && b[i + 1] == '0')
                       b[i] = 'a';
               }
           }
           Console.WriteLine("转换后的" + new string(b));
           return new string(b);
       }

你可能感兴趣的:(将数字转换为大写数字)