C#實現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字
更新時間:2024年01月18日 09:27:52 作者:wenchm
這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
一、對輸入數(shù)字的處理
用正則表達式對輸入的數(shù)字判斷是否符合貨幣格式,小數(shù)點前的數(shù)字串的長度是否不大于13。
二、源碼
1、Main()
// 貨幣金額小寫數(shù)字轉(zhuǎn)大寫漢字 // 小數(shù)點前數(shù)字長度<=13,即不超過十億 using System.Text.RegularExpressions; namespace NumtoUpperChinese { partial class Program { /// <summary> /// 判斷輸入的是否貨幣格式,是否小數(shù)點前<=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(); // 定義正則表達式模式 return regex.IsMatch(input); // 返回匹配結(jié)果 } [GeneratedRegex(@"^\d{0,13}(\.\d+)?$")] private static partial Regex MyRegex(); } } //運行結(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; // 是否是負數(shù) if (LowerMoney.Trim()[..1] == "-") { // 是負數(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 "負" + ReturnValue; } else { return ReturnValue; } } } }
以上就是C#實現(xiàn)貨幣金額數(shù)字轉(zhuǎn)大寫漢字的詳細內(nèi)容,更多關(guān)于C#數(shù)字轉(zhuǎn)大寫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12c#winform窗口頁面一打開就加載的實現(xiàn)方式
這篇文章主要介紹了c#winform窗口頁面一打開就加載的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06