网上搜的都有问题,这个代码属于原创,代码简洁,支持小数点,若没有小数点,则为整
/// <summary>
/// 将金额转换成大写
/// </summary>
/// <param name="moneys">字符串金额</param>
/// <returns></returns>
public static string GetUpperMoney(string money)
{
int dotPos = money.IndexOf('.');//小数点位置
string moneyRight = dotPos > - ? (money + "").Substring(dotPos + , ) : "";//小数部分
if (dotPos > -)
{
money = money.Substring(, dotPos);//money取整
}
string rtnStr = "";
string daxie = "元拾佰仟万亿";
string daxieZ = "零壹贰叁肆伍陆柒捌玖";
int moneyLen = money.Length;
for (int i = ; i < moneyLen; i++)
{
string nowStr = money.Substring(i, );
string mn1 = daxieZ.Substring(Convert.ToInt32(nowStr), );
int nowPos = moneyLen - i - ;
if (nowPos > )
{
if (nowPos == )
{
nowPos = ;
}
else
{
nowPos = nowPos % + ;
}
}
else if (nowPos > )
{
nowPos = nowPos % + ;
}
string mn2 = daxie.Substring(nowPos, );
rtnStr = rtnStr + mn1 + mn2;
} rtnStr = rtnStr.Replace("零仟", "零");
rtnStr = rtnStr.Replace("零佰", "零");
rtnStr = rtnStr.Replace("零拾", "零");
rtnStr = rtnStr.Replace("零零零", "零");
rtnStr = rtnStr.Replace("零零", "零");
rtnStr = rtnStr.Replace("零万", "万");
rtnStr = rtnStr.Replace("零亿", "亿");
if (dotPos > )
{
rtnStr = rtnStr + daxieZ.Substring(Convert.ToInt32(moneyRight.Substring(, )), ) + "角";
rtnStr = rtnStr + daxieZ.Substring(Convert.ToInt32(moneyRight.Substring(, )), ) + "分";
}
else
{
rtnStr += "整";
}
return rtnStr;
}