本文主要是介绍将金额转换成繁体汉字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>
/// 将金额转换成繁体汉字
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
private string ConvertAmountToChineseCharacters(string amount)
{
//标识是否是负数
bool IsNegative = false;
if (amount.Trim().Substring(0, 1) == "-")
{
//是负数则先转为正数
amount = amount.Trim().Remove(0, 1);
IsNegative = true;
}
string strAmount = string.Empty;
string strMiddleware = string.Empty;
string strChineseCharacter = string.Empty;
int iTemp = 0;
//保留两位小数
amount = Math.Round(double.Parse(amount), 2).ToString();
if (amount.IndexOf(".") > 0)
{
if (amount.IndexOf(".") == amount.Length - 2)
{
amount = amount + "0";
}
}
else
{
amount = amount + ".00";
}
strAmount = amount;
iTemp = 1;
strChineseCharacter = "";
while (iTemp <= strAmount.Length)
{
switch (strAmount.Substring(strAmount.Length - iTemp, 1))
{
case ".":
strMiddleware = "圆";
break;
case "0":
strMiddleware = "零";
break;
case "1":
strMiddleware = "壹";
break;
case "2":
strMiddleware = "贰";
break;
case "3":
strMiddleware = "叁";
break;
case "4":
strMiddleware = "肆";
break;
case "5":
strMiddleware = "伍";
break;
case "6":
strMiddleware = "陆";
break;
case "7":
strMiddleware = "柒";
break;
case "8":
strMiddleware = "捌";
break;
case "9":
strMiddleware = "玖";
break;
}
switch (iTemp)
{
case 1:
strMiddleware = strMiddleware + "分";
break;
case 2:
strMiddleware = strMiddleware + "角";
break;
case 3:
strMiddleware = strMiddleware + "";
break;
case 4:
strMiddleware = strMiddleware + "";
break;
case 5:
strMiddleware = strMiddleware + "拾";
break;
case 6:
strMiddleware = strMiddleware + "佰";
break;
case 7:
strMiddleware = strMiddleware + "仟";
break;
case 8:
strMiddleware = strMiddleware + "万";
break;
case 9:
strMiddleware = strMiddleware + "拾";
break;
case 10:
strMiddleware = strMiddleware + "佰";
break;
case 11:
strMiddleware = strMiddleware + "仟";
break;
case 12:
strMiddleware = strMiddleware + "亿";
break;
case 13:
strMiddleware = strMiddleware + "拾";
break;
case 14:
strMiddleware = strMiddleware + "佰";
break;
case 15:
strMiddleware = strMiddleware + "仟";
break;
case 16:
strMiddleware = strMiddleware + "万";
break;
default:
strMiddleware = strMiddleware + "";
break;
}
strChineseCharacter = strMiddleware + strChineseCharacter;
iTemp = iTemp + 1;
}
strChineseCharacter = strChineseCharacter.Replace("零拾", "零");
strChineseCharacter = strChineseCharacter.Replace("零佰", "零");
strChineseCharacter = strChineseCharacter.Replace("零仟", "零");
strChineseCharacter = strChineseCharacter.Replace("零零零", "零");
strChineseCharacter = strChineseCharacter.Replace("零零", "零");
strChineseCharacter = strChineseCharacter.Replace("零角零分", "整");
strChineseCharacter = strChineseCharacter.Replace("零分", "整");
strChineseCharacter = strChineseCharacter.Replace("零角", "零");
strChineseCharacter = strChineseCharacter.Replace("零亿零万零圆", "亿圆");
strChineseCharacter = strChineseCharacter.Replace("亿零万零圆", "亿圆");
strChineseCharacter = strChineseCharacter.Replace("零亿零万", "亿");
strChineseCharacter = strChineseCharacter.Replace("零万零圆", "万圆");
strChineseCharacter = strChineseCharacter.Replace("零亿", "亿");
strChineseCharacter = strChineseCharacter.Replace("零万", "万");
strChineseCharacter = strChineseCharacter.Replace("零圆", "圆");
strChineseCharacter = strChineseCharacter.Replace("零零", "零");
//对壹圆以下的金额的处理
if (strChineseCharacter.Substring(0, 1) == "圆")
{
strChineseCharacter = strChineseCharacter.Substring(1, strChineseCharacter.Length - 1);
}
if (strChineseCharacter.Substring(0, 1) == "零")
{
strChineseCharacter = strChineseCharacter.Substring(1, strChineseCharacter.Length - 1);
}
if (strChineseCharacter.Substring(0, 1) == "角")
{
strChineseCharacter = strChineseCharacter.Substring(1, strChineseCharacter.Length - 1);
}
if (strChineseCharacter.Substring(0, 1) == "分")
{
strChineseCharacter = strChineseCharacter.Substring(1, strChineseCharacter.Length - 1);
}
if (strChineseCharacter.Substring(0, 1) == "整")
{
strChineseCharacter = "零圆整";
}
if (IsNegative == true)
{
return "负" + strChineseCharacter;
}
else
{
return strChineseCharacter;
}
}
这篇关于将金额转换成繁体汉字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!