📄 numtoch.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace NumToCHN
{
public static class NumToCh
{
private static char ToNum(char x) //转换数字
{
string stringChnNames = "零壹贰叁肆伍陆柒捌玖";
string stringNumNames = "0123456789";
return stringChnNames[stringNumNames.IndexOf(x)];
}
public static string ToNumFive(string x) //转换万以下整数
{
string[] stringArrayLevelNames = new string[4] { "", "拾", "佰", "仟" };
string ret = "";
int i;
for (i = x.Length - 1; i >= 0; i--)
if (x[i] == '0')
ret = ToNum(x[i]) + ret;
else
ret = ToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
while ((i = ret.IndexOf("零零")) != -1)
ret = ret.Remove(i, 1);
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
ret = ret.Remove(ret.Length - 1, 1);
if (ret.Length >= 2 && ret.Substring(0, 2) == "壹拾")
ret = ret.Remove(0, 1);
return ret;
}
public static string ToNumInt(string x)
{
int len = x.Length;
string ret, temp;
if (len <= 4)
ret = ToNumFive(x);
else if (len <= 8)
{
ret = ToNumFive(x.Substring(0, len - 4)) + "万";
temp = ToNumFive(x.Substring(len - 4, 4));
if (temp.IndexOf("仟") == -1 && temp != "")
ret += "零" + temp;
else
ret += temp;
}
else
{
ret = ToNumFive(x.Substring(0, len - 8)) + "亿";
temp = ToNumFive(x.Substring(len - 8, 4));
if (temp.IndexOf("仟") == -1 && temp != "")
ret += "零" + temp;
else
ret += temp;
ret += "万";
temp = ToNumFive(x.Substring(len - 4, 4));
if (temp.IndexOf("仟") == -1 && temp != "")
ret += "零" + temp;
else
ret += temp;
}
int i;
if ((i = ret.IndexOf("零万")) != -1)
ret = ret.Remove(i + 1, 1);
while ((i = ret.IndexOf("零零")) != -1)
ret = ret.Remove(i, 1);
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
ret = ret.Remove(ret.Length - 1, 1);
return ret;
}
public static string ToNumZero(string x)
{
string ret = "";
for (int i = 0; i < x.Length; i++)
{
if (i == 0)
ret += ToNum(x[i]) + "角";
else
if (i == 1)
ret += ToNum(x[i]) + "分";
}
return ret;
}
public static String NumToChn(string x)
{
if (x.Length == 0)
return "";
string ret = "";
if (x[0] == '-')
{
ret = "负";
x = x.Remove(0, 1);
}
if (x[0].ToString() == ".")
x = "0" + x;
if (x[x.Length - 1].ToString() == ".")
x = x.Remove(x.Length - 1, 1);
if (x.IndexOf(".") > -1)
ret += ToNumInt(x.Substring(0, x.IndexOf("."))) + "圆" + ToNumZero(x.Substring(x.IndexOf(".") + 1));
else
ret += ToNumInt(x) + "圆整";
return ret;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -