数字金额转大写
C#数字金额转换大写金额
代码
#region 数字金额转大写
static string[] unums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
static string[] pos = { "拾", "佰", "仟", "万", "亿", "兆" };
static string[] units = { "元", "角", "分" };
public static void LowNumberToUpper()
{
string[] flag = { "." };
string intNum = string.Empty;
string decimalNum = string.Empty;
Console.Write("请输入小写数字金额:");
string input = Console.ReadLine();
try
{
input = Convert.ToDouble(input).ToString("#.00");
}
catch { }
//拆分小数部分
int n = input.IndexOf('.');
if (n == input.Length - 1)
{
intNum = input.Substring(0, n);
}
else if (n > 0 && n < input.Length)
{
intNum = input.Substring(0, n);
decimalNum = input.Substring(n + 1);
}
else
intNum = input;
//处理整数部分
int wei = intNum.Length;
int cut = 0;
string intNumString = string.Empty;
foreach (char c in intNum)
{
intNumString += unums[Convert.ToInt32(c.ToString())];
cut =1;
if (wei >= 13)
{
intNumString += GetUnit(wei - 12);
if (wei == 13){
intNumString += pos[5];
cut++;
}
}
else if (wei >= 9)
{
intNumString += GetUnit(wei - 8);
if (wei == 9){
intNumString += pos[4];
cut++;
}
}
else if (wei >= 5)
{
intNumString += GetUnit(wei - 4);
if (wei == 5){
intNumString += pos[3];
cut++;
}
}
else if (wei < 5)
intNumString += GetUnit(wei);
if (c == '0' && wei == 0)
{
intNumString = intNumString.Remove(intNumString.Length - 1);
}
if (c == '0' && wei != 0 && intNumString.Substring(intNumString.Length - 2, 1) == unums[0])
{
intNumString = intNumString.Remove(intNumString.Length - cut,1);
}
//去重
if (wei != 0 && intNumString.Substring(intNumString.Length - 2, 1) == intNumString.Substring(intNumString.Length - 1))
{
intNumString = intNumString.Remove(intNumString.Length - 1);
}
wei--;
}
if (intNumString.Substring(intNumString.Length - 1) == unums[0])
{
intNumString = intNumString.Remove(intNumString.Length - 1);
}
intNumString += units[0];
//处理小数部分
wei = decimalNum.Length;
string decimalNumString = string.Empty;
foreach (char c in decimalNum)
{
if (c != '0')
{
decimalNumString += unums[Convert.ToInt32(c.ToString())];
if (wei >= 2)
decimalNumString += units[1];
else if (wei >= 1)
decimalNumString += units[2];
}
wei--;
}
if (string.IsNullOrEmpty(decimalNum))
Console.WriteLine(intNum);
else
Console.WriteLine(intNum + "." + decimalNum);
Console.WriteLine(intNumString + decimalNumString);
}
private static string GetUnit(int wei)
{
string result = string.Empty;
if (wei >= 5)
result = pos[3];
else if (wei >= 4)
result = pos[2];
else if (wei >= 3)
result = pos[1];
else if (wei >= 2)
result = pos[0];
return result;
}
#endregion