目錄
一、對輸入數字的處理
二、源碼
1、Main()
2.類庫文章來源:http://www.zghlxwxcb.cn/news/detail-802359.html
一、對輸入數字的處理
????????用正則表達式對輸入的數字判斷是否符合貨幣格式,小數點前的數字串的長度是否不大于13。文章來源地址http://www.zghlxwxcb.cn/news/detail-802359.html
二、源碼
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;
}
}
}
}
到了這里,關于C#將貨幣金額數字轉大寫漢字的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!