欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现金额转换成中文大写金额

程序员文章站 2023-12-17 09:36:04
本文实例为大家分享了c#金额转换成中文大写金额的具体代码,供大家参考,具体内容如下 /// /// 金额转换成中文大写金额...

本文实例为大家分享了c#金额转换成中文大写金额的具体代码,供大家参考,具体内容如下

/// <summary>
  /// 金额转换成中文大写金额
  /// </summary>
  /// <param name="lowermoney">eg:10.74</param>
  /// <returns></returns>
  public static string moneytoupper(string lowermoney)
  {
   string functionreturnvalue = null;
   bool isnegative = false; // 是否是负数
   if (lowermoney.trim().substring(0, 1) == "-")
   {
    // 是负数则先转为正数
    lowermoney = lowermoney.trim().remove(0, 1);
    isnegative = true;
   }
   string strlower = null;
   string strupart = null;
   string strupper = null;
   int itemp = 0;
   // 保留两位小数 123.489→123.49  123.4→123.4
   lowermoney = math.round(double.parse(lowermoney), 2).tostring();
   if (lowermoney.indexof(".") > 0)
   {
    if (lowermoney.indexof(".") == lowermoney.length - 2)
    {
     lowermoney = lowermoney + "0";
    }
   }
   else
   {
    lowermoney = lowermoney + ".00";
   }
   strlower = lowermoney;
   itemp = 1;
   strupper = "";
   while (itemp <= strlower.length)
   {
    switch (strlower.substring(strlower.length - itemp, 1))
    {
     case ".":
      strupart = "圆";
      break;
     case "0":
      strupart = "零";
      break;
     case "1":
      strupart = "壹";
      break;
     case "2":
      strupart = "贰";
      break;
     case "3":
      strupart = "叁";
      break;
     case "4":
      strupart = "肆";
      break;
     case "5":
      strupart = "伍";
      break;
     case "6":
      strupart = "陆";
      break;
     case "7":
      strupart = "柒";
      break;
     case "8":
      strupart = "捌";
      break;
     case "9":
      strupart = "玖";
      break;
    }

    switch (itemp)
    {
     case 1:
      strupart = strupart + "分";
      break;
     case 2:
      strupart = strupart + "角";
      break;
     case 3:
      strupart = strupart + "";
      break;
     case 4:
      strupart = strupart + "";
      break;
     case 5:
      strupart = strupart + "拾";
      break;
     case 6:
      strupart = strupart + "佰";
      break;
     case 7:
      strupart = strupart + "仟";
      break;
     case 8:
      strupart = strupart + "万";
      break;
     case 9:
      strupart = strupart + "拾";
      break;
     case 10:
      strupart = strupart + "佰";
      break;
     case 11:
      strupart = strupart + "仟";
      break;
     case 12:
      strupart = strupart + "亿";
      break;
     case 13:
      strupart = strupart + "拾";
      break;
     case 14:
      strupart = strupart + "佰";
      break;
     case 15:
      strupart = strupart + "仟";
      break;
     case 16:
      strupart = strupart + "万";
      break;
     default:
      strupart = strupart + "";
      break;
    }

    strupper = strupart + strupper;
    itemp = itemp + 1;
   }

   strupper = strupper.replace("零拾", "零");
   strupper = strupper.replace("零佰", "零");
   strupper = strupper.replace("零仟", "零");
   strupper = strupper.replace("零零零", "零");
   strupper = strupper.replace("零零", "零");
   strupper = strupper.replace("零角零分", "整");
   strupper = strupper.replace("零分", "整");
   strupper = strupper.replace("零角", "零");
   strupper = strupper.replace("零亿零万零圆", "亿圆");
   strupper = strupper.replace("亿零万零圆", "亿圆");
   strupper = strupper.replace("零亿零万", "亿");
   strupper = strupper.replace("零万零圆", "万圆");
   strupper = strupper.replace("零亿", "亿");
   strupper = strupper.replace("零万", "万");
   strupper = strupper.replace("零圆", "圆");
   strupper = strupper.replace("零零", "零");

   // 对壹圆以下的金额的处理
   if (strupper.substring(0, 1) == "圆")
   {
    strupper = strupper.substring(1, strupper.length - 1);
   }
   if (strupper.substring(0, 1) == "零")
   {
    strupper = strupper.substring(1, strupper.length - 1);
   }
   if (strupper.substring(0, 1) == "角")
   {
    strupper = strupper.substring(1, strupper.length - 1);
   }
   if (strupper.substring(0, 1) == "分")
   {
    strupper = strupper.substring(1, strupper.length - 1);
   }
   if (strupper.substring(0, 1) == "整")
   {
    strupper = "零圆整";
   }
   functionreturnvalue = strupper;

   if (isnegative == true)
   {
    return "负" + functionreturnvalue;
   }
   else
   {
    return functionreturnvalue;
   }
 }

decimal pricesum = 32957.2654;

调用  var pricesumchinese = moneytoupper(pricesum.tostring());

结果:叁万贰仟玖佰伍拾柒圆贰角柒分

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: