目录

一、对输入数字的处理

二、源码

1、Main()

2.类库


一、对输入数字的处理

        用正则表达式对输入的数字判断是否符合货币格式,小数点前的数字串的长度是否不大于13。

二、源码

1、Main()

// 货币金额小写数字转大写汉字
// 小数点前数字长度<=13,即不超过十亿
using System.Text.RegularExpressions;

namespace NumtoUpperChinese
{
    partial class Program
    {
        /// <summary>
        /// 判断输入的是否货币格式,是否小数点前<=13,
        /// </summary>
        /// <param name="args"></param>
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);
            Console.WriteLine("请输入要判断的字符串(货币格式)");
            string input = Console.ReadLine()!.ToString();
            
            if (input!="") 
            {
                bool isValidFormat = IsValidCurrencyFormat(input);
                if (isValidFormat)
                {
                    Console.WriteLine(NumtoUpper.MoneyToUpper(input));
                }
                else
                {
                    Console.WriteLine("输入的货币格式无效");
                }

            }
            else
            {
                Console.WriteLine("输入数字不能空,请重新输入!", "提示");
            }
        }
        static bool IsValidCurrencyFormat(string input)
        {
            Regex regex = MyRegex();      // 定义正则表达式模式
            return regex.IsMatch(input);    // 返回匹配结果
        }

        [GeneratedRegex(@"^\d{0,13}(\.\d+)?$")]
        private static partial Regex MyRegex();
    }
}
//运行结果:
/*
请输入要判断的字符串(货币格式)
9999999999999.99
玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖圆玖角玖分

 */

2.类库

// 类库
namespace NumtoUpperChinese
{
    internal static class NumtoUpper
    {
        /// <summary>
        /// 金额转换成中文大写金额
        /// </summary>
        /// <param name="LowerMoney">eg:10.74</param>
        /// <returns></returns>
        public static string MoneyToUpper(string LowerMoney)
        {           
            string? ReturnValue;
            bool IsNegative = false; // 是否是负数
            if (LowerMoney.Trim()[..1] == "-")
            {
                // 是负数则先转为正数
                LowerMoney = LowerMoney.Trim().Remove(0, 1);
                IsNegative = true;
            }
            string? strLower;
            string? strUpart = null;
            string? strUpper;
            int iTemp;
            // 保留两位小数 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 += ('0');
                }
            }
            else
            {
                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;
                }

                strUpart = iTemp switch
                {
                    1 => strUpart + "分",
                    2 => strUpart + "角",
                    3 => strUpart + "",
                    4 => strUpart + "",
                    5 => strUpart + "拾",
                    6 => strUpart + "佰",
                    7 => strUpart + "仟",
                    8 => strUpart + "万",
                    9 => strUpart + "拾",
                    10 => strUpart + "佰",
                    11 => strUpart + "仟",
                    12 => strUpart + "亿",
                    13 => strUpart + "拾",
                    14 => strUpart + "佰",
                    15 => strUpart + "仟",
                    16 => strUpart + "万",
                    _ => strUpart + "",
                };
                strUpper = strUpart + strUpper;
                iTemp++;
            }

            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[..1] == "圆")
            {
                strUpper = strUpper[1..];
            }
            if (strUpper[..1] == "零")
            {
                strUpper = strUpper[1..];
            }
            if (strUpper[..1] == "角")
            {
                strUpper = strUpper[1..];
            }
            if (strUpper[..1] == "分")
            {
                strUpper = strUpper[1..];
            }
            if (strUpper[..1] == "整")
            {
                strUpper = "零圆整";
            }
            ReturnValue = strUpper;

            if (IsNegative == true)
            {
                return "负" + ReturnValue;
            }
            else
            {
                return ReturnValue;
            }
        }
    }
}
01-18 20:56