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;
}
/// <summary>
/// 检测是否翻译0(4位数)
/// </summary>
/// <param name="a">翻译的话返回0;不翻译返回a</param>
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);
}
将数字转换为大写数字
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...