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

本文介绍了一种将阿拉伯数字转换为中文大写数字的方法。通过C#编程实现,能够处理多位数字,并考虑了多种特殊情况,例如零的读法等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

结果如下:

 

 

 

代码如下:

 

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;
        }
    }
}

转载于:https://www.cnblogs.com/HCVOLCANO/archive/2008/08/28/1278414.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值