目前試用了個還沒有發現什麼問題,感覺還行吧,所以發來供大家參考
1. 引用命名空間:using System.Text.RegularExpressions;(操作正則表達式)
2. 小寫轉大寫
public static string sToh(string ch)
{
string str = "";
switch(ch)
{
case "1":
str = "壹";
break;
case "2":
str = "貳";
break;
case "3":
str = "參";
break;
case "4":
str = "肆";
break;
case "5":
str = "伍";
break;
case "6":
str = "陸";
break;
case "7":
str = "柒";
break;
case "8":
str = "捌";
break;
case "9":
str = "玖";
break;
default:
str = "零";
break;
}
return str;
}
{
string str = "";
switch(ch)
{
case "1":
str = "壹";
break;
case "2":
str = "貳";
break;
case "3":
str = "參";
break;
case "4":
str = "肆";
break;
case "5":
str = "伍";
break;
case "6":
str = "陸";
break;
case "7":
str = "柒";
break;
case "8":
str = "捌";
break;
case "9":
str = "玖";
break;
default:
str = "零";
break;
}
return str;
}
3. 獲得數位位數
private static string getBit(int ilen)
{
string str = "";
switch(ilen)
{
case 2:
str = "拾";
break;
case 3:
str = "佰";
break;
case 4:
str = "仟";
break;
case 5:
str = "萬";
break;
case 6:
str = "拾";
break;
case 7:
str = "佰";
break;
case 8:
str = "仟";
break;
case 9:
str = "億";
break;
case 10:
str = "拾";
break;
case 11:
str = "佰";
break;
case 12:
str = "仟";
break;
case 13:
str = "兆";
break;
case 14:
str = "拾";
break;
case 15:
str = "佰";
break;
case 16:
str = "仟";
break;
default:
str = "";
break;
}
{
string str = "";
switch(ilen)
{
case 2:
str = "拾";
break;
case 3:
str = "佰";
break;
case 4:
str = "仟";
break;
case 5:
str = "萬";
break;
case 6:
str = "拾";
break;
case 7:
str = "佰";
break;
case 8:
str = "仟";
break;
case 9:
str = "億";
break;
case 10:
str = "拾";
break;
case 11:
str = "佰";
break;
case 12:
str = "仟";
break;
case 13:
str = "兆";
break;
case 14:
str = "拾";
break;
case 15:
str = "佰";
break;
case 16:
str = "仟";
break;
default:
str = "";
break;
}
4. 轉換
1public static string ConventGig(string money)
2 {
3 string strTemp = money;
4 string Decimal = ""; //小數
5 string strValue = "";
6 string strDecimal = "";
7 if( money.IndexOf(".")>0)
8 {
9 strDecimal = "點";
10 strTemp=money.Substring(0,money.IndexOf("."));
11 Decimal = money.Substring(money.IndexOf(".")+1);
12 }
13 try
14 {
15 strTemp = Convert.ToInt64(strTemp).ToString();
16 }
17 catch
18 {
19 return "輸入不正確的數據!";
20 }
21 int iLen = strTemp.Length;
22 if(strTemp.Length>16) return "數據過大無法轉換";
23 for(int i=0; i<strTemp.Length; i++)
24 {
25 strValue += sToh(strTemp.Substring(i,1)) + getBit(iLen);
26 iLen--;
27 }
28 //小數
29 for(int i=0; i<Decimal.Length; i++)
30 {
31 strDecimal += sToh(Decimal.Substring(i,1));
32 }
33 Regex regex = new Regex("零+[拾,佰,仟,萬,億,兆]");
34 strValue = regex.Replace(strValue,"零");
35 regex = new Regex("零{2,16}");
36 strValue = regex.Replace(strValue,"零");
37 regex = new Regex("(萬|億|兆){1}零");
38 strValue = regex.Replace(strValue,"$1");
39 regex = new Regex("點$");
40 strDecimal = regex.Replace(strDecimal,"");
41 regex = new Regex("零+$");
42 strDecimal = regex.Replace(strDecimal,"");
43 return strValue + strDecimal;
44 }
2 {
3 string strTemp = money;
4 string Decimal = ""; //小數
5 string strValue = "";
6 string strDecimal = "";
7 if( money.IndexOf(".")>0)
8 {
9 strDecimal = "點";
10 strTemp=money.Substring(0,money.IndexOf("."));
11 Decimal = money.Substring(money.IndexOf(".")+1);
12 }
13 try
14 {
15 strTemp = Convert.ToInt64(strTemp).ToString();
16 }
17 catch
18 {
19 return "輸入不正確的數據!";
20 }
21 int iLen = strTemp.Length;
22 if(strTemp.Length>16) return "數據過大無法轉換";
23 for(int i=0; i<strTemp.Length; i++)
24 {
25 strValue += sToh(strTemp.Substring(i,1)) + getBit(iLen);
26 iLen--;
27 }
28 //小數
29 for(int i=0; i<Decimal.Length; i++)
30 {
31 strDecimal += sToh(Decimal.Substring(i,1));
32 }
33 Regex regex = new Regex("零+[拾,佰,仟,萬,億,兆]");
34 strValue = regex.Replace(strValue,"零");
35 regex = new Regex("零{2,16}");
36 strValue = regex.Replace(strValue,"零");
37 regex = new Regex("(萬|億|兆){1}零");
38 strValue = regex.Replace(strValue,"$1");
39 regex = new Regex("點$");
40 strDecimal = regex.Replace(strDecimal,"");
41 regex = new Regex("零+$");
42 strDecimal = regex.Replace(strDecimal,"");
43 return strValue + strDecimal;
44 }
5. 試用
0.1000500 輸出的結果為 >> 零點壹零零零伍
1008000500452008.12345 輸出的結果為 >> 壹仟零捌兆伍億肆拾伍萬貳仟零捌點壹貳參肆伍