詳細介紹C#基於純數學方法遞迴實現貨幣數字轉換中文功能的案例

來源:互聯網
上載者:User
這篇文章主要介紹了C#基於純數學方法遞迴實現貨幣數字轉換中文功能,涉及C#針對字串的遍曆、轉換與數學運算相關操作技巧,需要的朋友可以參考下

本文執行個體講述了C#基於純數學方法遞迴實現貨幣數字轉換中文功能。分享給大家供大家參考,具體如下:

最近由於項目的原因,需要寫一個貨幣數字轉換中文的演算法,先在網了找了一下,結果發現無一列外都是用(Replace)替換的方式來實現的,所以想寫個另外的演算法;因為本人是學數學出身的,所以用純數學的方法實現。

注意:本文中的演算法支援小於1023 (也就是9999億兆)貨幣數字轉化。

貨幣中文說明: 在說明代碼之前,首先讓我們回顧一下貨幣的讀法。

10020002.23 讀為 壹仟零貳萬零貳元貳角三分
1020 讀為 壹仟零貳拾元整。
100000 讀為 拾萬元整
0.13 讀為 壹角三分

代碼:

測試工程

static void Main(string[] args){Console.WriteLine("請輸入金額");string inputNum = Console.ReadLine();while (inputNum != "exit"){//貨幣數字轉化類NumCast nc = new NumCast();if (nc.IsValidated<string>(inputNum)){try{string chineseCharacter = nc.ConvertToChinese(inputNum);Console.WriteLine(chineseCharacter);}catch (Exception er){Console.WriteLine(er.Message);}}else{Console.WriteLine("不合法的數字或格式");}Console.WriteLine("\n請輸入金額");inputNum = Console.ReadLine();}Console.ReadLine();}

貨幣轉化類(NumCast類)功能介紹

1. 常量的規定

/// <summary>/// 數位/// </summary>public enum NumLevel { Cent, Chiao, Yuan, Ten, Hundred, Thousand, TenThousand, hundredMillon, Trillion };/// <summary>/// 數位的指數/// </summary>private int[] NumLevelExponent = new int[] { -2, -1, 0, 1, 2, 3, 4, 8, 12 };/// <summary>/// 數位的中文字元/// </summary>private string[] NumLeverChineseSign = new string[] { "分", "角", "元", "拾", "佰", "仟", "萬", "億", "兆" };/// <summary>/// 大寫字元/// </summary>private string[] NumChineseCharacter = new string[] {"零","壹","貳","三","肆","伍","陸","柒","捌","玖"};/// <summary>/// 整(當沒有 角分 時)/// </summary>private const string EndOfInt = "整";

2. 數字合法性驗證,採用Regex驗證

/// <summary>/// 正則表達驗證數字是否合法/// </summary>/// <param name="Num"></param>/// <returns></returns>public bool IsValidated<T>(T Num){Regex reg = new Regex(@"^(([0])|([1-9]\d{0,23}))(\.\d{1,2})?$");if (reg.IsMatch(Num.ToString())){return true;}return false;}

3. 擷取數位 例如 1000的數位為 NumLevel.Thousand

/// <summary>/// 擷取數位數位使用log/// </summary>/// <param name="Num"></param>/// <returns></returns>private NumLevel GetNumLevel(double Num){double numLevelLength;NumLevel NLvl = new NumLevel();if (Num > 0){numLevelLength = Math.Floor(Math.Log10(Num));for (int i = NumLevelExponent.Length - 1; i >= 0; i--){if (numLevelLength >= NumLevelExponent[i]){NLvl = (NumLevel)i;break;}}}else{NLvl = NumLevel.Yuan;}return NLvl;}

4. 判斷數字之間是否有跳位,也就是中文中間是否要加零,例如1020 就應該加零。

/// <summary>/// 是否跳位/// </summary>/// <returns></returns>private bool IsDumpLevel(double Num){ if (Num > 0){NumLevel? currentLevel = GetNumLevel(Num);NumLevel? nextLevel = null;int numExponent = this.NumLevelExponent[(int)currentLevel];double postfixNun = Math.Round(Num % (Math.Pow(10, numExponent)),2);if(postfixNun> 0)nextLevel = GetNumLevel(postfixNun);if (currentLevel != null && nextLevel != null){if (currentLevel > nextLevel + 1){return true;}}}return false;}

5. 把長數字分割為兩個較小的數字數組,例如把9999億兆,分割為9999億和0兆,因為電腦不支援過長的數字。

/// <summary>/// 是否大於兆,如果大於就把字串分為兩部分,/// 一部分是兆以前的數字/// 另一部分是兆以後的數字/// </summary>/// <param name="Num"></param>/// <returns></returns>private bool IsBigThanTillion(string Num){bool isBig = false;if (Num.IndexOf('.') != -1){//如果大於兆if (Num.IndexOf('.') > NumLevelExponent[(int)NumLevel.Trillion]){isBig = true;}}else{//如果大於兆if (Num.Length > NumLevelExponent[(int)NumLevel.Trillion]){isBig = true;}}return isBig;}/// <summary>/// 把數字字串由‘兆'分開兩個/// </summary>/// <returns></returns>private double[] SplitNum(string Num){//兆的開始位double[] TillionLevelNums = new double[2];int trillionLevelLength;if (Num.IndexOf('.') == -1)trillionLevelLength = Num.Length - NumLevelExponent[(int)NumLevel.Trillion];elsetrillionLevelLength = Num.IndexOf('.') - NumLevelExponent[(int)NumLevel.Trillion];//兆以上的數字TillionLevelNums[0] = Convert.ToDouble(Num.Substring(0, trillionLevelLength));//兆以下的數字TillionLevelNums[1] = Convert.ToDouble(Num.Substring(trillionLevelLength ));return TillionLevelNums;}

6. 是否以“壹拾”開頭,如果是就可以把它變為“拾”

bool isStartOfTen = false;while (Num >=10){if (Num == 10){isStartOfTen = true;break;}//Num的數位NumLevel currentLevel = GetNumLevel(Num);int numExponent = this.NumLevelExponent[(int)currentLevel];Num = Convert.ToInt32(Math.Floor(Num / Math.Pow(10, numExponent)));if (currentLevel == NumLevel.Ten && Num == 1){isStartOfTen = true;break;}}return isStartOfTen;

7. 合并大於兆連個數組轉化成的貨幣字串

/// <summary>/// 合并分開的數組中文貨幣字元/// </summary>/// <param name="tillionNums"></param>/// <returns></returns>private string ContactNumChinese(double[] tillionNums){string uptillionStr = CalculateChineseSign(tillionNums[0], NumLevel.Trillion, true, IsStartOfTen(tillionNums[0]));string downtrillionStr = CalculateChineseSign(tillionNums[1], null, true,false);string chineseCharactor = string.Empty;//分開後的字元是否有跳位if (GetNumLevel(tillionNums[1] * 10) == NumLevel.Trillion){chineseCharactor = uptillionStr + NumLeverChineseSign[(int)NumLevel.Trillion] + downtrillionStr;}else{chineseCharactor = uptillionStr + NumLeverChineseSign[(int)NumLevel.Trillion];if (downtrillionStr != "零元整"){chineseCharactor += NumChineseCharacter[0] + downtrillionStr;}else{chineseCharactor += "元整";}}return chineseCharactor;}

8. 遞迴計算貨幣數位中文

/// <summary>/// 計算中文字串/// </summary>/// <param name="Num">數字</param>/// <param name="NL">數位層級 比如1000萬的 數位層級為萬</param>/// <param name="IsExceptTen">是否以‘壹拾'開頭</param>/// <returns>中文大寫</returns>public string CalculateChineseSign(double Num, NumLevel? NL ,bool IsDump,bool IsExceptTen){Num = Math.Round(Num, 2);bool isDump = false;//Num的數位NumLevel? currentLevel = GetNumLevel(Num);int numExponent = this.NumLevelExponent[(int)currentLevel];string Result = string.Empty;//整除後的結果int prefixNum;//餘數 當為小數的時候 分子分母各乘100double postfixNun ;if (Num >= 1){prefixNum = Convert.ToInt32(Math.Floor(Num / Math.Pow(10, numExponent)));postfixNun = Math.Round(Num % (Math.Pow(10, numExponent)), 2);}else{prefixNum = Convert.ToInt32(Math.Floor(Num*100 / Math.Pow(10, numExponent+2)));postfixNun = Math.Round(Num * 100 % (Math.Pow(10, numExponent + 2)), 2);postfixNun *= 0.01;}if (prefixNum < 10 ){//避免以‘壹拾'開頭if (!(NumChineseCharacter[(int)prefixNum] == NumChineseCharacter[1]&& currentLevel == NumLevel.Ten && IsExceptTen)){Result += NumChineseCharacter[(int)prefixNum];}else{IsExceptTen = false;}//加上單位if (currentLevel == NumLevel.Yuan ){////當為 “元” 位不為零時 加“元”。if (NL == null){Result += NumLeverChineseSign[(int)currentLevel];//當小數點後為零時 加 "整"if (postfixNun == 0){Result += EndOfInt;}}}else{Result += NumLeverChineseSign[(int)currentLevel];} //當真正的個位為零時加上“元”if (NL == null && postfixNun < 1 && currentLevel > NumLevel.Yuan && postfixNun > 0){Result += NumLeverChineseSign[(int)NumLevel.Yuan];}}else{//當 首碼數字未被除盡時, 遞迴下去NumLevel? NextNL = null;if ((int)currentLevel >= (int)(NumLevel.TenThousand))NextNL = currentLevel;Result += CalculateChineseSign((double)prefixNum, NextNL, isDump, IsExceptTen);if ((int)currentLevel >= (int)(NumLevel.TenThousand)){Result += NumLeverChineseSign[(int)currentLevel];}}//是否跳位// 判斷是否加零, 比如302 就要給三百 後面加零,變為 三百零二。if (IsDumpLevel(Num)){Result += NumChineseCharacter[0];isDump = true;}//餘數是否需要遞迴if (postfixNun > 0){Result += CalculateChineseSign(postfixNun, NL, isDump, false);}else if (postfixNun == 0 && currentLevel > NumLevel.Yuan ){//當數字是以零元結尾的加上 元整 比如1000000一百萬元整if (NL == null){Result += NumLeverChineseSign[(int)NumLevel.Yuan];Result += EndOfInt;}}return Result;}

9. 外部調用的轉換方法。

/// <summary>/// 外部調用的轉換方法/// </summary>/// <param name="Num"></param>/// <returns></returns>public string ConvertToChinese(string Num){if (!IsValidated<string>(Num)){throw new OverflowException("數值格式不正確,請輸入小於9999億兆的數字且最多精確的分的金額!");}string chineseCharactor = string.Empty;if (IsBigThanTillion(Num)){double[] tillionNums = SplitNum(Num);chineseCharactor = ContactNumChinese(tillionNums);}else{double dNum = Convert.ToDouble(Num);chineseCharactor = CalculateChineseSign(dNum, null, true, IsStartOfTen(dNum));}return chineseCharactor;}

小結:

個人認為程式的靈魂是演算法,大到一個系統中的商務邏輯,小到一個貨幣數字轉中文的演算法,處處都體現一種邏輯思想。

是否能把需求抽象成一個好的數學模型,直接關係到程式的實現的複雜度和穩定性。在一些常用功能中想些不一樣的演算法,對我們開拓思路很有協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.