最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字

 更新時(shí)間:2024年01月18日 09:27:52   作者:wenchm  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、對輸入數(shù)字的處理

用正則表達(dá)式對輸入的數(shù)字判斷是否符合貨幣格式,小數(shù)點(diǎn)前的數(shù)字串的長度是否不大于13。

二、源碼

1、Main()

// 貨幣金額小寫數(shù)字轉(zhuǎn)大寫漢字
// 小數(shù)點(diǎn)前數(shù)字長度<=13,即不超過十億
using System.Text.RegularExpressions;
 
namespace NumtoUpperChinese
{
    partial class Program
    {
        /// <summary>
        /// 判斷輸入的是否貨幣格式,是否小數(shù)點(diǎn)前<=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("輸入數(shù)字不能空,請重新輸入!", "提示");
            }
        }
        static bool IsValidCurrencyFormat(string input)
        {
            Regex regex = MyRegex();      // 定義正則表達(dá)式模式
            return regex.IsMatch(input);    // 返回匹配結(jié)果
        }
 
        [GeneratedRegex(@"^\d{0,13}(\.\d+)?$")]
        private static partial Regex MyRegex();
    }
}
//運(yùn)行結(jié)果:
/*
請輸入要判斷的字符串(貨幣格式)
9999999999999.99
玖萬玖仟玖佰玖拾玖億玖仟玖佰玖拾玖萬玖仟玖佰玖拾玖圓玖角玖分
 */

2.類庫

// 類庫
namespace NumtoUpperChinese
{
    internal static class NumtoUpper
    {
        /// <summary>
        /// 金額轉(zhuǎn)換成中文大寫金額
        /// </summary>
        /// <param name="LowerMoney">eg:10.74</param>
        /// <returns></returns>
        public static string MoneyToUpper(string LowerMoney)
        {           
            string? ReturnValue;
            bool IsNegative = false; // 是否是負(fù)數(shù)
            if (LowerMoney.Trim()[..1] == "-")
            {
                // 是負(fù)數(shù)則先轉(zhuǎn)為正數(shù)
                LowerMoney = LowerMoney.Trim().Remove(0, 1);
                IsNegative = true;
            }
            string? strLower;
            string? strUpart = null;
            string? strUpper;
            int iTemp;
            // 保留兩位小數(shù) 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 "負(fù)" + ReturnValue;
            }
            else
            {
                return ReturnValue;
            }
        }
    }
}

以上就是C#實(shí)現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字的詳細(xì)內(nèi)容,更多關(guān)于C#數(shù)字轉(zhuǎn)大寫的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

陈巴尔虎旗| 台江县| 垣曲县| 高清| 怀远县| 镇安县| 乐业县| 淮安市| 筠连县| 炎陵县| 嵩明县| 剑川县| 高要市| 平安县| 自贡市| 甘孜| 宣汉县| 阿合奇县| 会泽县| 平谷区| 浦北县| 东乌| 宝丰县| 奇台县| 儋州市| 彭阳县| 和平县| 铜山县| 盱眙县| 安远县| 十堰市| 宣威市| 什邡市| 张掖市| 盘山县| 紫金县| 安阳市| 全南县| 江源县| 龙胜| 鲁甸县|