我的一个朋友写的,
/// <summary>
/// 金额转成大写
/// </summary>
/// <param name="dNum"></param>
/// <returns></returns>
public static string ConvertMoney(decimal dNum)
{
try
{
string strResult = "";
int iAddZero = 0;//加零标志
string[] str1 = new string[] {"分","角","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万","拾","佰","仟"};
string[] str2 = new string[] {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
string strUnit = string.Join("", str1);
string strUpperNum = string.Join("", str2);
decimal dNum2 = dNum;
//是否零 xyq 2006.5.18 add
if (dNum == 0)
return "零元";
//是否负数
bool bMinus = false;
if (dNum < 0)
{
dNum2 = Math.Abs(dNum2);
bMinus = true;
}
dNum2 = Math.Round(dNum2 * 100);
string strDigit = dNum2.ToString("0");
int iLength = strDigit.Length;
if (iLength > 15 || iLength < 0)//如果输入的数字超出double类型数据的长度的话返回“error“
throw new Exception("金额长度不能超出15位");
//将字符串逐位处理
for (int i = 0;i<iLength;i++)
{
int iDigit = int.Parse(strDigit.Substring(i, 1));
if (iDigit == 0)//如果当前位为零,则将加零标志+1
iAddZero++;
else
{
if (iAddZero > 0)//如果当前为不为0,且加0标志大于0
strResult += "零";//则在字符串中加入“零“
strResult += strUpperNum[iDigit];
iAddZero = 0;
}
//该位不为0||元位||亿位||万位
if ((iDigit != 0) || (iLength - i == 3) || (iLength - i == 11)||((iLength - i + 1)%8 == 0 && (iAddZero < 4)))
strResult += strUnit[iLength - i - 1];
}
if ((strDigit.Length >= 2) && (strDigit.Substring(strDigit.Length - 2,2) == "00"))
strResult += "整";
if (bMinus)
{
strResult = "负" + strResult;
}
return strResult;
}
catch(Exception ex)
{
throw ex;
}
}