小写金额转换为大写金额

我的一个朋友写的,

/// <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;
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值