当前位置:文档之家› 数字换成大写

数字换成大写

代码如下:



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



相关主题
文本预览
相关文档 最新文档