货币转大写两种方法
第一种
public static string GetMoney(double dd)
{
string s = dd.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
string value = Regex.Replace(d, ".", delegate(Match m)
{ return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
return value;
}
{
string s = dd.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
string value = Regex.Replace(d, ".", delegate(Match m)
{ return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
return value;
}
第二种
static public string convertMoneytoRMB(double decMoney)
{
string strMoney, strOneNum, strTemp, strConverted;
int i, iLen;
//设初值
strConverted = "";
strMoney = decMoney.ToString();
iLen = strMoney.Length;
//先取小数位
if (strMoney.IndexOf(".") > 0)
{
strTemp = strMoney.Substring(strMoney.IndexOf(".") + 1, strMoney.Length - strMoney.IndexOf(".") - 1);
if (strTemp.Length > 2)
{
Console.WriteLine("错误:无法计算超过2位的小数");
return strConverted;
}
else if (strTemp == "0" || strTemp == "00" || strTemp == "")
strTemp = "";
else
{
if (strTemp.Length == 1 && strTemp != "0")
{
strConverted = converNumtoCapital(strTemp) + "角" + strConverted;
}
else
{
strOneNum = strTemp.Substring(0, 1);
strConverted = converNumtoCapital(strOneNum) + (strOneNum != "0" ? "角" : "") + strConverted;
strOneNum = strTemp.Substring(1, 1);
strConverted = strConverted + (strOneNum != "0" ? converNumtoCapital(strOneNum) + "分" : "");
}
}
}
//取整数部分
if (strMoney.IndexOf(".") < 0)
strTemp = strMoney;
else
strTemp = strMoney.Substring(0, strMoney.IndexOf("."));
iLen = strTemp.Length;
Console.WriteLine(strTemp);
if (iLen > 0 && decimal.Parse(strTemp) != 0)
{
strConverted = "元" + strConverted;
for (i = 0; i < iLen; ++i)
{
strOneNum = strTemp.Substring(iLen - 1 - i, 1);
if ((i + 1) == 1)
{
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + strConverted;
}
else if (((i + 1) % 4 == 2 || (i + 1) == 2) && i % 4 != 0 && i % 8 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "拾" + strConverted;
}
else if (((i + 1) % 4 == 3 || (i + 1) == 3) && i % 4 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "佰" + strConverted;
}
else if ((i + 1) % 4 == 0 && i % 4 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "千" + strConverted;
}
else if (i % 4 == 0 && i % 8 != 0)
{
Console.WriteLine("万位{0}", i);
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "万" + strConverted;
}
else if (i % 8 == 0)
{
Console.WriteLine("亿位{0}", i);
if (strConverted.Substring(0, 1) == "万") strConverted = strConverted.Substring(1, strConverted.Length - 1);
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "亿" + strConverted;
}
else
{
Console.WriteLine(i);
strConverted = converNumtoCapital(strOneNum) + strConverted;
}
}
}
return strConverted;
}
static string converNumtoCapital(string strNum)
{
string strCapital = "";
switch (strNum)
{
case "0":
strCapital = "零";
break;
case "1":
strCapital = "壹";
break;
case "2":
strCapital = "贰";
break;
case "3":
strCapital = "叁";
break;
case "4":
strCapital = "肆";
break;
case "5":
strCapital = "伍";
break;
case "6":
strCapital = "陆";
break;
case "7":
strCapital = "柒";
break;
case "8":
strCapital = "捌";
break;
case "9":
strCapital = "玖";
break;
default:
strCapital = "";
break;
}
return strCapital;
}
{
string strMoney, strOneNum, strTemp, strConverted;
int i, iLen;
//设初值
strConverted = "";
strMoney = decMoney.ToString();
iLen = strMoney.Length;
//先取小数位
if (strMoney.IndexOf(".") > 0)
{
strTemp = strMoney.Substring(strMoney.IndexOf(".") + 1, strMoney.Length - strMoney.IndexOf(".") - 1);
if (strTemp.Length > 2)
{
Console.WriteLine("错误:无法计算超过2位的小数");
return strConverted;
}
else if (strTemp == "0" || strTemp == "00" || strTemp == "")
strTemp = "";
else
{
if (strTemp.Length == 1 && strTemp != "0")
{
strConverted = converNumtoCapital(strTemp) + "角" + strConverted;
}
else
{
strOneNum = strTemp.Substring(0, 1);
strConverted = converNumtoCapital(strOneNum) + (strOneNum != "0" ? "角" : "") + strConverted;
strOneNum = strTemp.Substring(1, 1);
strConverted = strConverted + (strOneNum != "0" ? converNumtoCapital(strOneNum) + "分" : "");
}
}
}
//取整数部分
if (strMoney.IndexOf(".") < 0)
strTemp = strMoney;
else
strTemp = strMoney.Substring(0, strMoney.IndexOf("."));
iLen = strTemp.Length;
Console.WriteLine(strTemp);
if (iLen > 0 && decimal.Parse(strTemp) != 0)
{
strConverted = "元" + strConverted;
for (i = 0; i < iLen; ++i)
{
strOneNum = strTemp.Substring(iLen - 1 - i, 1);
if ((i + 1) == 1)
{
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + strConverted;
}
else if (((i + 1) % 4 == 2 || (i + 1) == 2) && i % 4 != 0 && i % 8 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "拾" + strConverted;
}
else if (((i + 1) % 4 == 3 || (i + 1) == 3) && i % 4 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "佰" + strConverted;
}
else if ((i + 1) % 4 == 0 && i % 4 != 0)
{
if (strOneNum == "0")
{
if (strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿")
continue;
else
strConverted = "零" + strConverted;
}
else
strConverted = converNumtoCapital(strOneNum) + "千" + strConverted;
}
else if (i % 4 == 0 && i % 8 != 0)
{
Console.WriteLine("万位{0}", i);
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "万" + strConverted;
}
else if (i % 8 == 0)
{
Console.WriteLine("亿位{0}", i);
if (strConverted.Substring(0, 1) == "万") strConverted = strConverted.Substring(1, strConverted.Length - 1);
strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "亿" + strConverted;
}
else
{
Console.WriteLine(i);
strConverted = converNumtoCapital(strOneNum) + strConverted;
}
}
}
return strConverted;
}
static string converNumtoCapital(string strNum)
{
string strCapital = "";
switch (strNum)
{
case "0":
strCapital = "零";
break;
case "1":
strCapital = "壹";
break;
case "2":
strCapital = "贰";
break;
case "3":
strCapital = "叁";
break;
case "4":
strCapital = "肆";
break;
case "5":
strCapital = "伍";
break;
case "6":
strCapital = "陆";
break;
case "7":
strCapital = "柒";
break;
case "8":
strCapital = "捌";
break;
case "9":
strCapital = "玖";
break;
default:
strCapital = "";
break;
}
return strCapital;
}