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());

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

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

(0)

相关推荐

  • C#中利用正则表达式将人民币金额转换为大写汉字

    直接来看代码: public static string ConvertToChineseMoney(double money) { if (money < 0) throw new ArgumentOutOfRangeException("参数money不能为负值!"); string s = money.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); s

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

    本文实例为大家分享了C#金额转换成中文大写金额的具体代码,供大家参考,具体内容如下 /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param name="LowerMoney">eg:10.74</param> /// <returns></returns> public static string MoneyToUpper(string LowerMoney)

  • PHP数字金额转换成中文大写显示

    整个功能其实不难,只是还算挺实用,所以记一下哈,其他编程语言转换一下也是能可以的! 思路:把传过来的金额转换成整数和小数两部分,再对其分别进行转换处理 代码附上: function moneyToString($num) { $digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; $radices =['', '拾', '佰', '仟', '万', '亿']; $bigRadices = ['', '万', '亿']; $dec

  • asp.net(vb)实现金额转换成大写的函数

    复制代码 代码如下: ''' <summary> ''' 将数据转换为Double数字 ''' </summary> ''' <param name="Obj"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function todbl(ByVal Obj As Object) As Double

  • Python输出\u编码将其转换成中文的实例

    爬取了下小猪短租的网站出租房信息但是输出的时候是这种: 百度了下.python2.7在window上的编码确实是个坑 解决如下 如果是个字典的话要先将其转成字符串 导入json库 然后 这么输出(json.dumps(data).decode("unicode-escape")) 整个代码demo # -*- coding: UTF-8 -*- #小猪短租爬取 import requests from bs4 import BeautifulSoup import json def g

  • Python3的unicode编码转换成中文的问题及解决方案

    这篇文章主要介绍了Python3的unicode编码转换成中文的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 从别的地方搬过来的,担心以后不容易搜索到,就收集过来. 我当时面临的问题是要从C++发json代码出来,用python写了个server,然后返回给C++程序,结果收到的是: httpSvrDataCbUser: {"tranNO": "0808ad498670dc996", "d

  • vue-element-admin如何转换成中文

    目录 vue-element-admin转换成中文 vue-element-admin中文问题 vue-element-admin转换成中文 从admin官网上git下来的默认是英文,是修改不了语言的. 如果需要转换成中文,在vscode终端里git checkout i18n,然后npm install,npm run dev之后就是中文的了 如图: vue-element-admin中文问题 今天刚看到vue-element-admin项目,下载下来后发现是英文的 要切换成中文,需要下载它的

  • python中将阿拉伯数字转换成中文的实现代码

    复制代码 代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass class OutOfRangeError(Exception): pass _MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', ) _P0 = (u'', u'十', u'百', u'千', )

  • JavaScript实现把数字转换成中文

    var _change = {            ary0:["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],            ary1:["", "十", "百", "千&qu

  • JS实现金额转换(将输入的阿拉伯数字)转换成中文的实现代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="

  • Python实现把数字转换成中文

    周末在家,写了个小程序,用于将阿拉伯数字转换化大写中文.程序没经过任何优化,出没经过详细的测试,挂到网上,方便将来有需要的时候直接拿来用. #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass class OutOfRangeError(Exception): pass _MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五'

随机推荐