数字金额大写转换器制作代码分享(人民币大写转换)

时间:2021-05-25 11:51:25 


/// <summary>
/// 人民币大写
/// </summary>
/// <param name="input">待转换输入</param>
/// <param name="prefix">需要添加人民币前缀</param>
/// <exception cref="ArgumentException" />
/// <returns>转换后的结果</returns>
public static string ToCapital(this string input, bool prefix = false) {

    #region Step1 输入有效性验证

    if (!Regex.IsMatch(input, @"(?<=-|^)\d*\.?\d*$"))
        throw new ArgumentException("错误的输入金额!");

    if (Regex.IsMatch(input, @"^\d{25,}"))
        throw new ArgumentException("输入数据太大无法转换!");

    #endregion

    #region Step2 格式化为中间字符串

    var positionDic = new Dictionary<int, string> {
        {0,"F"},{1,"J"},{2,"."},{3,"S"},{4,"B"},{5,"Q"},{6,"W"},{7,"SW"},{8,"BW"},{9,"QW"},
        {10,"Y"},{11,"SY"},{12,"BY"},{13,"QY"},{14,"WY"},{15,"SWY"},{16,"BWY"},{17,"QWY"},{18,"YY"},
        {19,"SYY"},{20,"BYY"},{21,"QYY"},{22,"WYY"},{23,"SWYY"},{24,"BWYY"},{25,"QWYY"}               
    };

    input = Regex.Replace(input, @"^\.", "0.");

    var integerPart = Regex.Replace(input, @"^-|\..*$", "");
    var _matchDecimal = Regex.Match(input, @"\.\d*$", RegexOptions.None);
    var decimalPart = Regex.Replace(_matchDecimal.Success ? Convert.ToDouble(_matchDecimal.Value).ToString("0.00") : "00", @"0\.", "");

    var processStack = new Stack<string>();
    var charsArray = (integerPart + decimalPart).Reverse<char>();
    for (int i = 0; i < charsArray.Count(); i++) {
        processStack.Push(string.Format("{0}{1}", charsArray.ElementAt(i), positionDic[i]));
    }

    //符号处理
    if (Regex.IsMatch(input, "^-", RegexOptions.None)) {
        processStack.Push("-");
    }
    if (prefix) {
        processStack.Push("¥");
    }

    var process = string.Empty;
    while (processStack.Count > 0) {
        process += processStack.Pop();
    }
    //语义处理模式队列
    Queue<Tuple<string, string, MatchEvaluator>> patterns = new Queue<Tuple<string, string, MatchEvaluator>>();
    var patternBuilder = new StringBuilder();
    for (int i = 3; i < positionDic.Count; i++) {
        patternBuilder.AppendFormat("{0}{1}", (i == 3 ? "(0(?:" : "") + positionDic[i], i == positionDic.Count - 1 ? ")+?)+" : "|");
    }
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(patternBuilder.ToString(), "0", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?YY)+", null, m => m.Value.Replace("YY", "") + "YY"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?Y)+", null, m => m.Value.Replace("Y", "") + "Y"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:Q|B|S)?\d?W)+", null, m => m.Value.Replace("W", "") + "W"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?!^)0+\.", ".", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0\.0J|^0\.", "", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("0J|0F", "0", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("J0?$", "JZ", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"\.$|\.0+$", ".Z", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0+$|^[^.]{0}$", "0.Z", null));

    while (patterns.Count > 0) {
        var pattern = patterns.Dequeue();
        if (pattern.Item3 != null) {
            process = Regex.Replace(process, pattern.Item1, pattern.Item3);
        }
        else {
            process = Regex.Replace(process, pattern.Item1, pattern.Item2);
        }
    }

    #endregion

    #region Step3 翻译中间字符串
    StringBuilder result = new StringBuilder();
    var translatorDic = new Dictionary<char, string> {
        {'0',"零"},{'1',"壹"},{'2',"贰"},{'3',"叁"},{'4',"肆"},{'5',"伍"},{'6',"陆"},{'7',"柒"},{'8',"捌"},{'9',"玖"},
        {'S',"拾"},{'B',"佰"},{'Q',"仟"},{'W',"萬"},{'Y',"亿"},
        {'¥',"人民币"},{'-',"负"},{'.',"圆"},{'J',"角"},{'F',"分"},{'Z',"整"}
    };
    for (int i = 0; i < process.Length; i++) {
        result.Append(translatorDic[process[i]]);
    }
    #endregion

    return result.ToString();
}

标签:人民币大写转换,数字金额大写转换器
0
投稿

猜你喜欢

  • Android中用Bmob实现短信验证码功能的方法详解

    2022-06-08 22:00:25
  • C#文件断点续传实现方法

    2023-09-07 18:35:05
  • Java基础详解之内存泄漏

    2022-04-14 03:45:46
  • java 实现线程同步的方式有哪些

    2023-01-13 01:26:14
  • android studio2.3如何编译动态库的过程详解

    2023-07-11 03:47:48
  • 搞懂Java线程池

    2021-08-04 10:01:06
  • Elasticsearch配置文件示例示范

    2021-11-05 22:59:31
  • Spring Shell应用程序开发流程解析

    2021-06-28 23:20:50
  • Android批量插入数据到SQLite数据库的方法

    2022-09-28 06:18:06
  • 详解三种java实现多线程的方式

    2021-11-02 10:09:30
  • 详解Spring Boot集成MyBatis(注解方式)

    2023-10-03 17:45:47
  • Android4.2中全屏或者取消标题栏的方法总结

    2023-06-14 16:17:06
  • 详解Android通知栏沉浸式/透明化完整解决方案

    2023-09-06 03:59:11
  • C#日历样式的下拉式计算器实例讲解

    2022-05-07 20:11:33
  • c#中的常用ToString()方法总结

    2023-01-13 01:18:30
  • Java中ArrayList和LinkedList区别

    2023-09-06 20:43:09
  • RocketMQ源码解析topic创建机制详解

    2023-03-16 01:06:23
  • 浅谈Action+Service +Dao 功能

    2023-01-04 13:44:20
  • android中RecyclerView自定义分割线实现

    2023-08-03 17:37:47
  • 详解SpringMVC如何进行数据回显

    2023-09-12 08:48:15
  • asp之家 软件编程 m.aspxhome.com