其他分享
首页 > 其他分享> > 将数字金额转换成中文大写格式

将数字金额转换成中文大写格式

作者:互联网

using System;

namespace ConsoleApp1
{
	public static class MyExtension
{
		public static string toCapitalMoney(this string strMoney)
		{
			if (string.IsNullOrEmpty(strMoney))
				return string.Empty;

			var money = Convert.ToDouble(strMoney);
			if (money >= 999999999999999.9999)
				return string.Empty;
			else if (money == 0)
				return "零元整";

			var cnNums = "零壹贰叁肆伍陆柒捌玖";
			var cnIntRadice = ",拾,佰,仟".Split(',');
			var cnIntUnits = ",万,亿,兆".Split(',');
			var cnDecUnits = "角分毫厘";
			var cnInteger = '整';
			var cnIntLast = '元';

			var chineseStr = "";
			var decimalNum = string.Empty;      // 小数部分
			var parts = money.ToString().Split('.');
			var integerNum = parts[0];
			if (parts.Length > 1)
				decimalNum = parts[1].Length > 4 ? parts[1].Substring(0, 4) : parts[1];

			var intNum = int.Parse(integerNum);
			if (intNum > 0)
			{
				var zeroCount = 0;
				var intLen = integerNum.Length;
				for (var i = 0; i < intLen; i++)
				{
					var n = integerNum[i];
					var p = intLen - i - 1;
					var q = p / 4;
					var m = p % 4;
					if (n == '0')
					{
						zeroCount++;
					}
					else
					{
						if (zeroCount > 0)
							chineseStr += cnNums[0];

						zeroCount = 0;
						chineseStr += cnNums[n - '0'] + cnIntRadice[m];
					}

					if (m == 0 && zeroCount < 4)
					{
						chineseStr += cnIntUnits[q];
					}
				}

				chineseStr += cnIntLast;
			}

			if (decimalNum != "")
			{
				var decLen = decimalNum.Length;
				for (var i = 0; i < decLen; i++)
				{
					var n = decimalNum[i];
					if (n != '0')
						chineseStr += cnNums[n - '0'].ToString() + cnDecUnits[i];
				}
			}

			if (chineseStr == "")
			{
				chineseStr += cnNums[0] + cnIntLast + cnInteger;
			}
			else if (decimalNum == "")
			{
				chineseStr += cnInteger;
			}

			return chineseStr;
		}
	}

	class Program
	{
		static void Main(string[] args)
		{
			var list = new string[] {
			"",
			"0",
			"12",
			"120",
			"102",
			"2102",
			"1002",
			"1200",
			"12000",
			"120000",
			"120000",
			"120000",
			"120000",
			"120300",
			"1001201000",
			"140120010",
			"12.",
			"12.1",
			"14.36",
			"14.236",
			"14.2563",
			"14.23565",
			".2356",
			"0.235",
			};

			foreach (var i in list)
			{
				var bigs = i.toCapitalMoney();
				Console.WriteLine("{0} \t\t=\t\t{1}", i, bigs);
			}
		}
	}
}

运行结果如下:

                =
0               =               零元整
12              =               壹拾贰元整
120             =               壹佰贰拾元整
102             =               壹佰零贰元整
2102            =               贰仟壹佰零贰元整
1002            =               壹仟零贰元整
1200            =               壹仟贰佰元整
12000           =               壹万贰仟元整
120000          =               壹拾贰万元整
120000          =               壹拾贰万元整
120000          =               壹拾贰万元整
120000          =               壹拾贰万元整
120300          =               壹拾贰万零叁佰元整
1001201000              =               壹拾亿零壹佰贰拾万零壹仟元整
140120010               =               壹亿肆仟零壹拾贰万零壹拾元整
12.             =               壹拾贰元整
12.1            =               壹拾贰元壹角
14.36           =               壹拾肆元叁角陆分
14.236          =               壹拾肆元贰角叁分陆毫
14.2563                 =               壹拾肆元贰角伍分陆毫叁厘
14.23565                =               壹拾肆元贰角叁分伍毫陆厘
.2356           =               贰角叁分伍毫陆厘
0.235           =               贰角叁分伍毫
请按任意键继续. . .

标签:转换成,string,120000,大写,chineseStr,壹拾,var,格式,元整
来源: https://blog.csdn.net/JoeBlackzqq/article/details/121445927