如何把阿拉伯數字轉換爲中文大寫?

    之前在CSDN論壇裏,看有人Show了一些把中文大寫轉換成阿拉伯數字的程序代碼,自己也用Java語言實現了程序設計,基本上能夠實現十萬億以內的任何數字的轉換。之前也帖出自己的實現代碼。

    但我個人認爲,把大寫轉換爲數字實際上是沒有多少意義的。因爲很少有把大寫轉爲數字的,大多數都是把數字轉換爲大寫。何況把大寫轉爲數字並不是一件很容易的事情,因爲漢語大寫有很多種說法,比如說:101,它的漢語說法可以有好多種:

    (一)、百零一

    (二)、一百零一

    (三)、一百又一

    (四)、幺零幺

    因此,要實現起來其實是很難的。而且還要防止用戶輸入大小寫不固定,一會輸入“一百零一”,一會又輸入“壹佰零壹”,或者是二者的雜合體。形式千變萬化,何況這一個數字就可以有這麼多種,還有那麼多特殊的數字呢?除非用成統一標準的輸入。否則是很容易出現問題的。

    但是,數字轉換成大寫卻是很實用的,平時去銀行用得比較多,只要涉及金錢的時候,基本上都要求用大寫。所以,這種轉換看來是符合要求,因此,我用C#代碼實現了其功能,並將用戶輸入用“,”分隔成三個一組的形式,可以實現百萬億級別的數字轉換。下面我帖出部分代碼,大家指正:

 

其他輸入驗證我就不帖出來了,只把核心代碼帖出來

  1. class Chinese
  2.     {
  3.         public string returnResult(long num)
  4.         {
  5.             string numStr = num.ToString();
  6.             //主要理念是把數字字符串最終分隔爲四個一組四個一組的,不足的不管。
  7.             if (numStr.Length > 8 & numStr.Length < 16)
  8.             {
  9.                 //定義一個數組,存儲整個字條串分隔後的字符串。然後兩次遞歸調用。
  10.                 //調用完全後,得到總的大寫字符串。
  11.                 string[] firstSplit = new string[2];
  12.                 firstSplit[0] = numStr.Substring(0, numStr.Length - 8);
  13.                 firstSplit[1] = numStr.Substring(numStr.Length - 8, 8);
  14.                 string result1 = getString(firstSplit[0]) + "億";
  15.                 string result2 = getString(firstSplit[1]);
  16.                 return result1 + result2;
  17.             }
  18.             else
  19.             {
  20.                 return getString(numStr);
  21.             }
  22.         }
  23.             //進行一次遞歸調用,得到兩組四位數字的大字字串
  24.         public string getString(string str)
  25.         {
  26.             if (str.Length > 4)
  27.             {
  28.                 string[] secondSplit = new string[2];
  29.                 secondSplit[0] = str.Substring(0, str.Length - 4);
  30.                 secondSplit[1] = str.Substring(str.Length - 4, 4);
  31.                 string result1 = getRe(secondSplit[0]);
  32.                 string result2 = getRe(secondSplit[1]);
  33.                 //確定是否加“萬”字
  34.                 if (!secondSplit[0].Equals("0000"))
  35.                 {
  36.                     result1 += "萬";
  37.                 }
  38.                 return result1 + result2;
  39.             }
  40.             else
  41.             {
  42.                 return getRe(str);
  43.             }
  44.         }
  45.         int[] value = { 1000, 100, 10 };
  46.         //將四位數以內的字符串返回爲正確的大寫稱呼 比如1234 返回:一千二百三十四
  47.         public string getRe(string doWith)
  48.         {
  49.             char[] number = doWith.ToCharArray();
  50.             int length = number.Length;
  51.             string re = "";
  52.             for (int i = 0; i < length; i++)
  53.             {
  54.                 switch (number[i])
  55.                 {
  56.                     case '0':
  57.                         if (re.EndsWith("零"))
  58.                         {
  59.                             re += "";
  60.                         }
  61.                         else
  62.                         {
  63.                             re += "零";
  64.                         }
  65.                         break;
  66.                     case '1':
  67.                         re += "壹";
  68.                         break;
  69.                     case '2':
  70.                         re += "貳";
  71.                         break;
  72.                     case '3':
  73.                         re += "叄";
  74.                         break;
  75.                     case '4':
  76.                         re += "肆";
  77.                         break;
  78.                     case '5':
  79.                         re += "伍";
  80.                         break;
  81.                     case '6':
  82.                         re += "陸";
  83.                         break;
  84.                     case '7':
  85.                         re += "柒";
  86.                         break;
  87.                     case '8':
  88.                         re += "捌";
  89.                         break;
  90.                     case '9':
  91.                         re += "玖";
  92.                         break;
  93.                 }
  94.                 int index = (int)Math.Pow(10, length - i - 1);
  95.                 if (number[i].ToString() == "0")
  96.                 {
  97.                     index = -1;
  98.                 }
  99.                 switch (index)
  100.                 {
  101.                     case 1000:
  102.                         re += "仟";
  103.                         break;
  104.                     case 100:
  105.                         re += "佰";
  106.                         break;
  107.                     case 10:
  108.                         re += "拾";
  109.                         break;
  110.                 }
  111.             }
  112.             if (re.EndsWith("零"))
  113.             {
  114.                 re = re.Substring(0, re.Length - 1);
  115.             }
  116.             return re;
  117.         }
  118.     }

 

CSDN下載地址:http://d.download.csdn.net/down/842756/yeah86

發佈了11 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章