2022-12-27 17:33:33 +08:00

340 lines
6.8 KiB
C#

using System;
namespace GoldPrinter
{
public class ChineseNum
{
public static string GetChineseNum(string p_num)
{
ChineseNum chineseNum = new ChineseNum();
return chineseNum.NumToChn(p_num);
}
public static string GetUpperMoney(double p_Money)
{
ChineseNum chineseNum = new ChineseNum();
return chineseNum.GetMoneyChinese(p_Money);
}
private char CharToNum(char x)
{
string text = "零一二三四五六七八九";
string text2 = "0123456789";
return text[text2.IndexOf(x)];
}
private string WanStrToInt(string x)
{
string[] array = new string[]
{
"",
"十",
"百",
"千"
};
string text = "";
int i;
for (i = x.Length - 1; i >= 0; i--)
{
if (x[i] == '0')
{
text = this.CharToNum(x[i]) + text;
}
else
{
text = this.CharToNum(x[i]) + array[x.Length - 1 - i] + text;
}
}
while ((i = text.IndexOf("零零")) != -1)
{
text = text.Remove(i, 1);
}
if (text[text.Length - 1] == '零' && text.Length > 1)
{
text = text.Remove(text.Length - 1, 1);
}
if (text.Length >= 2 && text.Substring(0, 2) == "一十")
{
text = text.Remove(0, 1);
}
return text;
}
private string StrToInt(string x)
{
int length = x.Length;
string text;
if (length <= 4)
{
text = this.WanStrToInt(x);
}
else if (length <= 8)
{
text = this.WanStrToInt(x.Substring(0, length - 4)) + "万";
string text2 = this.WanStrToInt(x.Substring(length - 4, 4));
if (text2.IndexOf("千") == -1 && text2 != "")
{
text = text + "零" + text2;
}
else
{
text += text2;
}
}
else
{
text = this.WanStrToInt(x.Substring(0, length - 8)) + "亿";
string text2 = this.WanStrToInt(x.Substring(length - 8, 4));
if (text2.IndexOf("千") == -1 && text2 != "")
{
text = text + "零" + text2;
}
else
{
text += text2;
}
text += "万";
text2 = this.WanStrToInt(x.Substring(length - 4, 4));
if (text2.IndexOf("千") == -1 && text2 != "")
{
text = text + "零" + text2;
}
else
{
text += text2;
}
}
int num;
if ((num = text.IndexOf("零万")) != -1)
{
text = text.Remove(num + 1, 1);
}
while ((num = text.IndexOf("零零")) != -1)
{
text = text.Remove(num, 1);
}
if (text[text.Length - 1] == '零' && text.Length > 1)
{
text = text.Remove(text.Length - 1, 1);
}
return text;
}
private string StrToDouble(string x)
{
string text = "";
for (int i = 0; i < x.Length; i++)
{
text += this.CharToNum(x[i]);
}
return text;
}
private string NumToChn(string x)
{
string result;
if (x.Length == 0)
{
result = "";
}
else
{
string text = "";
if (x[0] == '-')
{
text = "负";
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)
{
text = text + this.StrToInt(x.Substring(0, x.IndexOf("."))) + "点" + this.StrToDouble(x.Substring(x.IndexOf(".") + 1));
}
else
{
text += this.StrToInt(x);
}
result = text;
}
return result;
}
private string GetMoneyChinese(double Money)
{
string result;
if (Money == 0.0)
{
result = "";
}
else
{
string text = Money.ToString("#0.00");
int num = text.IndexOf(".");
if (num > 0)
{
text = text.Replace(".", "");
}
if (text.Substring(0, 1) == "0")
{
text = text.Remove(0, 1);
}
text = this.NumstrToChinese(text);
if (text.Length == 0)
{
result = "";
}
else
{
if (Money < 0.0)
{
text = "负" + text;
}
text = text.Replace("0", "零");
text = text.Replace("1", "壹");
text = text.Replace("2", "贰");
text = text.Replace("3", "叁");
text = text.Replace("4", "肆");
text = text.Replace("5", "伍");
text = text.Replace("6", "陆");
text = text.Replace("7", "柒");
text = text.Replace("8", "捌");
text = text.Replace("9", "玖");
text = text.Replace("M", "亿");
text = text.Replace("W", "万");
text = text.Replace("S", "仟");
text = text.Replace("H", "佰");
text = text.Replace("T", "拾");
text = text.Replace("Y", "圆");
text = text.Replace("J", "角");
text = text.Replace("F", "分");
if (text.Substring(text.Length - 1, 1) != "分")
{
text += "整";
}
result = text;
}
}
return result;
}
private string NumstrToChinese(string numstr)
{
string[] array = new string[4];
string text = "";
bool flag = false;
array[0] = "";
array[1] = "T";
array[2] = "H";
array[3] = "S";
for (int i = 1; i <= numstr.Length; i++)
{
int num = numstr.Length - i;
string text2 = numstr.Substring(i - 1, 1);
if (text2 != "0" && num > 1)
{
text = text + text2 + array[(num - 2) % 4];
}
if (text2 == "0" && !flag)
{
text += "0";
flag = true;
}
if (num == 14)
{
if (text.Substring(text.Length - 1) == "0")
{
text = text.Substring(0, text.Length - 1) + "W0";
}
else
{
text += "W";
}
}
if (num == 2)
{
if (text.Substring(text.Length - 1, 1) == "0")
{
text = text.Substring(0, text.Length - 1) + "Y0";
}
else
{
text += "Y";
}
}
if (num == 6)
{
if (text.Length > 2)
{
if (text.Substring(text.Length - 2) != "M0")
{
if (text.Substring(text.Length - 1) == "0")
{
text = text.Substring(0, text.Length - 1) + "W0";
}
else
{
text += "W";
}
}
}
else if (text.Substring(text.Length - 1) == "0")
{
text = text.Substring(0, text.Length - 1) + "W0";
}
else
{
text += "W";
}
}
if (num == 10)
{
if (text.Substring(text.Length - 1) == "0")
{
text = text.Substring(0, text.Length - 1) + "M0";
}
else
{
text += "M";
}
}
if (num == 0 && text2 != "0")
{
text = text + text2 + "F";
}
if (num == 1 && text2 != "0")
{
text = text + text2 + "J";
}
if (text2 != "0")
{
flag = false;
}
}
if (text.Substring(0, 1) == "1" && text.Substring(1, 1) == array[1])
{
text = text.Substring(1);
}
if (text.Substring(text.Length - 1, 1) == "0")
{
text = text.Substring(0, text.Length - 1);
}
if (text.Substring(0, 1) == "0")
{
text = text.Substring(1);
}
if (text.Substring(text.Length - 1, 1) == "M" || text.Substring(text.Length - 1, 1) == "W" || text.Substring(text.Length - 1, 1) == "S" || text.Substring(text.Length - 1, 1) == "H" || text.Substring(text.Length - 1, 1) == "T")
{
text += "Y";
}
return text;
}
}
}