[原创] C# 金额大写

突然要用到这个功能.也网上找了下. 最后还是自动动手写了一个.

估计这个还是有人要要的,所以顺便发出来吧.

 引用保留 https://www.cnblogs.com/goldli/p/14105832.html

using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace TruckScale.Common
{
    /// <summary>
    /// 将数字转为大写
    /// <remarks>https://www.cnblogs.com/goldli/p/14105832.html</remarks>
/// </summary> class ConvertToChineseMoney { private static Dictionary<char, string> _dic; private static Dictionary<int, string> _units; private void Init() { _dic = new Dictionary<char, string> { {0, ""}, {1, ""}, {2, ""}, {3, ""}, {4, ""}, {5, ""}, {6, ""}, {7, ""}, {8, ""}, {9, ""} }; _units = new Dictionary<int, string> { {1,""}, {2,""}, {3,""}, {4,""}, {5,""}, {6,""}, {7,""}, {8,""}, {9,"亿"}, {10,""}, {11,""}, {12,""}, {13,""}, {14,"亿"} }; } private ConvertToChineseMoney() { Init(); } public static ConvertToChineseMoney Ins { get; } = new ConvertToChineseMoney(); /// <summary> /// 对整数部分进行操作 /// </summary> /// <param name="source"></param> /// <returns></returns> private string ConvertIntegers(string source) { /* 示例: 120 5002 1002 5002 100 5002 */ var totalLen = source.Length; //总长度 var g = totalLen / 4; //中国数字以4个数字为一组。 这里取模。结果为 多出 的部分. var r = totalLen % 4; //取模后,得到的余数 var parts = new string[r > 0 ? g + 1 : g]; if (r > 0) //此时表示 不能整除 { parts[0] = source.Substring(0, r); //取出 for (var i = 0; i < g; i++) { parts[i + 1] = source.Substring(i * 4 + r, 4); //取出 } } else { for (var i = 0; i < parts.Length ; i++) { parts[i] = source.Substring(i * 4, 4); //取出 } } var result = new StringBuilder(); for (var i = 0; i < parts.Length; i++) { var part = parts[i]; var m0 = Regex.Match(part, "0{2,}"); //查找是否存在多个连续 0 的情况 if (m0.Success) //存在连续的多个 0, 如: 1002 { var index = m0.Index; /* -----------------简化代码见下面----------------- if (index == 0) // 00 开头 { result.Append("零"); totalLen -= 2; // 00后面的部分 for (var n = index + 2; n < 4; n++) { result.Append($"{_dic[part[n]]}{_units[totalLen]}"); totalLen--; } } else { // 00前面的部分 for (var n = 0; n < index; n++) { result.Append($"{_dic[part[n]]}{_units[totalLen]}"); totalLen--; } result.Append("零"); totalLen-=2; // 00后面的部分 for (var n = index + 2; n < 4; n++) { result.Append($"{_dic[part[n]]}{_units[totalLen]}"); totalLen--; } } */ if (index > 0) { for (var n = 0; n < index; n++) { result.Append($"{_dic[part[n]]}{_units[totalLen]}"); totalLen--; } } result.Append(""); totalLen -= m0.Length; for (var n = index + m0.Length; n < 4; n++) { result.Append($"{_dic[part[n]]}{_units[totalLen]}"); totalLen--; } } else { var x = part.Length; var y = part.TrimEnd(0).Length;//数字尾部的 0 if (y == 0) { result.Append(""); //0.95 的 0 totalLen--; } else if (y != x) { for (var m = 0; m < y; m++) { result.Append($"{_dic[part[m]]}{(part[m] == ‘0‘ ? "" : _units[totalLen])}"); //part[m] == ‘0‘ 零后面不需要 进制单位 totalLen--; } result.Append($"{_units[4 * (parts.Length - i - 1) + 1]}"); totalLen--; } else { for (var m = 0; m < y; m++) { result.Append($"{_dic[part[m]]}{(part[m] == ‘0‘ ? "" : _units[totalLen])}"); totalLen--; } } } } return result.ToString(); } public string Convert(double d) { var ret = new StringBuilder(); var source = d.ToString(CultureInfo.CurrentCulture); var dotIndex = source.IndexOf(.); if (dotIndex > 0)//有小数 { var integerPart = source.Substring(0, dotIndex); ret.Append(ConvertIntegers(integerPart)) .Append(""); var decimalPart = source.Substring(dotIndex + 1, source.Length - dotIndex -1); foreach (var c in decimalPart) { ret.Append($"{_dic[c]}"); } } else { ret.Append(ConvertIntegers(source)); } return ret.ToString(); } } }

用法 就简了.

            var s1 = ConvertToChineseMoney.Ins.Convert(278449980.95);
            var s2 = ConvertToChineseMoney.Ins.Convert(0.95);
            var s3 = ConvertToChineseMoney.Ins.Convert(1205020);
            var s4 = ConvertToChineseMoney.Ins.Convert(1205002);
            var s5 = ConvertToChineseMoney.Ins.Convert(1200052);
            var s6 = ConvertToChineseMoney.Ins.Convert(10025002);
            var s76 = ConvertToChineseMoney.Ins.Convert(100001734);

 

[原创] C# 金额大写

上一篇:定位元素的父(parent::)、兄弟(following-sibling::、preceding-sibling::)节点


下一篇:[c#]抽象类中子类方法重写的原理