一个用C#语言写的能将输入的阿拉伯数字转换为中文大写数字的方法(原创)

前段时间给学生出了一个题目,要求写一个能将输入的一个阿拉伯数字转换成中文大写数字的方法。很多学生都没有写出来,就算写出来的也出现了很多考虑不周到的地方。昨天我自己写了一个控制台应用程序,基本能符合题目的要求。

 

结果如下:

 

 

 

代码如下:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConvertNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            GetZWNum(Console.ReadLine());
            Console.ReadLine();
        }

       
        public static string GetZWNum(string strN)
        {
            string[] strNum = { "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            int bl = -1;
            bool ch = true;
            int len = strN.Length;
            if (len > 24)
            {
                Console.WriteLine("您输入的数字过大,无法转换!");
                return "";
            }
            string strResult = "";
            string[] strSZ = new string[len];
            for (int i = 0; i < len; i++)
            {
                strSZ[i] = strN.Substring(i, 1);
                if (!Regex.IsMatch(strSZ[i], "^[0-9]$"))
                {
                    Console.WriteLine("您输入的数字含有非数字符号!");
                    return "";
                }
                if (strSZ[0] == "0" && ch)//检验首位出现零的情况
                {
                    if (i != len - 1 && strSZ[i] == "0" && strSZ[i + 1] != "0")
                        bl = i;
                    else
                        ch = false;
                }
            }
            for (int i = 0; i < len; i++)
            {
                int num = len - i;
                if (strSZ[i] != "0")
                {
                    strResult += strNum[Convert.ToInt32(strSZ[i]) - 1];//将阿拉伯数字转换成中文大写数字
                    //加上单位
                    if (num % 4 == 2)
                        strResult += "拾";
                    if (num % 4 == 3)
                        strResult += "佰";
                    if (num % 4 == 0)
                        strResult += "仟";
                    if (num % 4 == 1)
                    {
                        if (num / 4 == 1)
                            strResult += "萬";
                        if (num / 4 == 2)
                            strResult += "亿";
                        if (num / 4 == 3)
                            strResult += "萬";
                        if (num / 4 == 4)
                            strResult += "亿";
                        if (num / 4 == 5)
                            strResult += "萬";
                    }
                }
                else
                {
                    if (i > bl)
                    {
                        if ((i != len - 1 && strSZ[i + 1] != "0" && (num - 1) % 4 != 0))
                        {
                            //此处判断“0”不是出现在末尾,且下一位也不是“0”;
                            //如 10012332 在此处读法应该为壹仟零壹萬贰仟叁佰叁拾贰,两个零只要读一个零
                            strResult += "零";
                        }
                        if (i != len - 1 && strSZ[i + 1] != "0")
                        {
                            switch (num)
                            {
                                //此处出现的情况是如 10002332,“0”出现在万位上就应该加上一个“萬”读成壹仟萬零贰仟叁佰叁拾贰
                                case 5: strResult += "萬";
                                    break;
                                case 9: strResult += "亿";
                                    break;
                                case 13: strResult += "萬";
                                    break;
                            }
                        }
                        if (i != len - 1 && strSZ[i + 1] != "0" && (num - 1) % 4 == 0)
                        {
                            //此处出现的情况是如 10002332,“0”出现在万位上就应该加上一个“零”读成壹仟萬零贰仟叁佰叁拾贰
                            strResult += "零";
                        }
                    }
                }
            }
            return strResult;
        }
    }
}

posted on 2008-08-28 10:43  HC  阅读(2297)  评论(1编辑  收藏  举报

导航