Loner_li 机试题 将数字 例如"12345.7895”,用程序实现显示"壹万贰仟叁佰肆拾伍圆柒角玖分"的方式

  

class Program

    {
        //"12345.7895”,用程序实现显示"壹万贰仟叁佰肆拾伍圆柒角玖分"的方式  
        static void Main(string[] args)
        {
            Console.WriteLine("请输入要转换的数字:");
            string str = Console.ReadLine();
            str.Replace(",", ","); str.Replace(",", "");
            //判断 小数点不能大于1个
            if (str.IndexOf(".") != str.LastIndexOf("."))
                return;
            // 获取小数点的位置
            int dot = str.IndexOf(".");
            if (dot == -1)
                dot = str.Length;
            //以小数点位置将数字分为两组

            char[] charsA = str.Substring(0, dot).ToCharArray();
            string[] strLimtA = { "元", "拾", "佰", "仟", "万" };
            Array.Reverse(charsA);

            char[] charsB = str.Substring(dot + 1).ToCharArray();
            string[] strLimtB = { "角", "分" };
            //将数字数据 读取到汉字
            string strRead = string.Empty;
            if (charsA.Length <= 5 )
            {
                for (int i = dot - 1; i > -1; i--)
                {
                    strRead += intToChs(charsA[i]);
                    strRead += strLimtA[i];
                }
                for (int i = 0; i < charsB.Length; i++)
                {
                    if (i >2) break;
                    if (i == 2)
                        strRead += "...";
                    else
                    {
                        strRead += intToChs(charsB[i]);
                        strRead += strLimtB[i];
                    }
                }
                Console.WriteLine(strRead);
            }
            else
                Console.WriteLine("请不要输入超过万分位的数字!");
            Console.ReadKey();
            }
        public static string intToChs(char str)
        {
            switch (str)
            {
                case '1':
                    str = '壹';
                    break;
                case '2':
                    str = '贰';
                    break;
                case '3':
                    str = '叁';
                    break;
                case '4':
                    str = '肆';
                    break;
                case '5':
                    str = '伍';
                    break;
                case '6':
                    str = '陆';
                    break;
                case '7':
                    str = '柒';
                    break;
                case '8':
                    str = '捌';
                    break;
                case '9':
                    str = '玖';
                    break;
                case '0':
                    str = '零';
                    break;
                default:
                    break;
            }
            return str.ToString();
        }

    }

你可能感兴趣的:(Loner_li 机试题 将数字 例如"12345.7895”,用程序实现显示"壹万贰仟叁佰肆拾伍圆柒角玖分"的方式)