人民币金额由阿拉伯数值转换成汉字大写数值的函数
分成四個步驟:
1。數字的轉化。阿拉伯數字轉換為漢字大寫字符
2。整數部分的轉化。
?????? 分節:四位一組,從個位開始分節
?????? 每節轉化為漢字大寫數值:比如“1234”轉化成?“壹仟貳佰叁拾肆萬”
?????? 每節加上單位:
????? 補零:
????? 整合成最后的數值:
3。?小數不份轉化。
????? 十分位轉化為‘角’
????? 百分位轉化為‘分’
????? 其他位不轉化????
/// <summary>
??????? /// 判斷是否是數字字符串
??????? /// </summary>
??????? /// <param name="str">字符串</param>
??????? /// <returns>true 是數字字符串,false 不是數字字符串</returns>
??????? private bool JudgeNumber(string str)
??????? {
??????????? //用正則表達式的方式判斷
??????????? System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");
??????????? return reg.IsMatch(str);
??????? }
??????? /// <summary>
??????? /// 將阿拉伯字符裝換位漢字大寫字符,比如‘1’轉化為‘壹’
??????? /// </summary>
??????? /// <param name="str">阿拉伯字符</param>
??????? /// <returns>漢字大寫字符</returns>
??????? private string convertnumtochs(string str)
??????? {
??????????? string[] arr = new string[10] {"零" ,"壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" };
??????????? string result="";
??????????? switch (str)
??????????????? {
??????????????????? case "0":
??????????????????????? result = arr[0];
??????????????????????????? break;
??????????????????? case "1":
??????????????????????????? result =? arr[1];
??????????????????????????? break;
??????????????????? case "2":
??????????????????????????? result =? arr[2];
??????????????????????????? break;
??????????????????? case "3":
??????????????????????????? result =? arr[3];
??????????????????????????? break;
??????????????????? case "4":
??????????????????????????? result =? arr[4];
??????????????????????????? break;
??????????????????? case "5":
??????????????????????????? result =arr[5];
??????????????????????????? break;
??????????????????? case "6":
??????????????????????????? result =? arr[6];
??????????????????????????? break;
??????????????????? case "7":
??????????????????????????? result = arr[7];
??????????????????????????? break;
??????????????????? case "8":
??????????????????????????? result =arr[8];
??????????????????????????? break;
??????????????????? case "9":
??????????????????????????? result = arr[9];
??????????????????????????? break;
??????????????? }???????????????
??????????? return result;
??????? }
??????? /// <summary>
??????? /// 將四位阿拉伯數字轉化成漢字大寫數字,比如'1234' 轉化成 '壹仟貳佰叁拾肆'
??????? /// </summary>
??????? /// <param name="num">阿拉伯數字</param>
??????? /// <returns>漢字大寫字符</returns>
??????? string convertfournumber(string num)
??????? {
??????????? //判斷是否阿拉伯數字組成的字符串
??????????? if (!JudgeNumber(num))
??????????????? return "";
??????????? //字符串長度超過4個,或者為空,都不處理。
??????????? if (num.Length > 4 || num.Length < 1)
??????????????? return "";
??????????? //不足四位的前面補零
??????????? if (num.Length < 4)
??????????? {
??????????????? num = "0000" + num;
??????????????? num = num.Substring(num.Length - 4, 4);
??????????? }
??????????? //特殊情況
??????????? if (num == "0000")
??????????????? return "";
??????????? //臨時變量
??????????? string result = "";
??????????? string temp = "";
??????????? string temp1, temp2, temp3;
??????????? //處理四位阿拉伯數字
??????????? //保留各位字符
??????????? temp = num.Substring(0, 1);
??????????? temp1 = num.Substring(1, 1);
??????????? temp2 = num.Substring(2, 1);
??????????? temp3 = num.Substring(3, 1);
??????????? //處理千位,為零不處理
??????????? if (temp != "0")
?????????????? result = convertnumtochs(temp) + "仟";
??????????? //一起出來十位,百位
??????????? if (temp1 != "0" && temp2 != "0")??? //百位,十位都是非零數字
??????????????? result = result + convertnumtochs(temp1) + "佰" + convertnumtochs(temp2) + "拾";
??????????? else
??????????? {
??????????????? if (temp1 == "0" && temp2 == "0")?? //百位,十位都為零,但是千位,個位都不為零
??????????????? {
??????????????????? if(temp != "0" && temp3 != "0")
??????????????????????? result = result + "零";//補個零
??????????????? }
??????????????? else
??????????????? {
??????????????????? if (temp1 == "0")? //百位為零,十位不為零,千位不為零
??????????????????? {
??????????????????????? if(temp != "0")
????????????????????????????? result = result + "零" + convertnumtochs(temp2) + "拾";? //千位和十位間補零
??????????????????????? else
??????????????????????????? result = result +? convertnumtochs(temp2) + "拾";? //千位和十位間補零
??????????????????? }
??????????????????? else//百位不為零,十位為零
??????????????????? {
??????????????????????? if (temp3 != "0")//個位不為零
??????????????????????????? result = result + convertnumtochs(temp1) + "佰" + "零"; //百位和個位間補零
??????????????????????? else
??????????????????????????? result = result + convertnumtochs(temp1) + "佰" ; //百位和個位間補零
??????????????????? }
??????????????? }
??????????? }
??????????? //處理個位,為零不處理
??????????? temp = num.Substring(3, 1);
??????????? if (temp != "0")
??????????????? result =result+convertnumtochs(temp) ;
??????????? //返回值
??????????? return result;???????????????
??????? }
??????? /// <summary>
??????? /// 將一個數字字符串按照指定長度,和方向,截取成一段段的字符串,比如說123456789,截取長度為4,
??????? /// 從右到左,截取三個字符串,'1','2345','6789'
??????? /// </summary>
??????? /// <param name="str">被截取的字符串</param>
??????? /// <param name="length">截取長度</param>
??????? /// <param name="type">true:從左往右. false:從右到左</param>
??????? /// <returns>分段的列表</returns>
??????? IList<string> breakstring(string str, int length, bool type)
??????? {
??????????? //保存返回結果
??????????? IList<string> list = new List<string>();
??????????? //兩種特殊情況,1.截取的字符長度小于1,被截取的字符串長度小于1
??????????? if (length < 1)
??????????????? return list;
??????????? if (str.Length < 1)
??????????????? return list;
??????????? //截取的長度大于被截取的字符串長度
??????????? if (length >= str.Length)
??????????? {
??????????????? list.Add(str);
??????????????? return list;
??????????? }
??????????? string temp;
??????????? int positon = 0;//初始位置為零
??????????? int i = 0;//截取次數的標量
??????????? //從左往右截取字符串
??????????? if (type)
??????????? {
??????????????? positon = 0;//初始位置為零
??????????????? i = 0;//截取次數的標量
??????????????? while (positon < str.Length)
??????????????? {
??????????????????? temp = str.Substring(positon, length);
??????????????????? i++;
??????????????????? if (temp != "")
??????????????????????? list.Add(temp);
??????????????????? else
??????????????????????? break;
??????????????????? positon = positon +? length;
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? positon = str.Length - length;
??????????????? if (positon > length*(-1) && positon < 0)//如果str長度小于length長度
??????????????????? positon = 0;
??????????????? i = 0;
??????????????? while (positon >= 0)
??????????????? {
??????????????????? if (str.Length > length)
??????????????????????? temp = str.Substring(positon, length);
??????????????????? else
??????????????????????? temp = str;
??????????????????? i++;
??????????????????? str = str.Substring(0, str.Length - temp.Length);
??????????????????? if (temp != "")
??????????????????????? list.Add(temp);//保存在列表中的順序是反的
??????????????????? else
??????????????????????? break;
??????????????????? positon = positon - length;
??????????????????? if (positon > length * (-1) && positon < 0)//被截取字符串最后剩下的長度不到截取的長度
??????????????????????? positon = 0;
??????????????? }
??????????? }
??????????? return list;
??????? }
??????? /// <summary>
??????? /// 將一個阿拉伯整數轉化成漢字大寫數字字符串,處理的最大數字單位為千萬億億
??????? /// </summary>
??????? /// <param name="str">阿拉伯字符串</param>
??????? /// <returns>漢字大寫數字字符串</returns>
??????? string intconvert(string str)
??????? {
??????????? //特殊情況
??????????? if (!JudgeNumber(str)) //判斷字符串中有沒有非數字字符
??????????????? return "";
??????????? if (str.Length < 1) //字符串為空
??????????????? return "";
??????????? //從右往左截斷字符串,四位為一組分節
??????????? IList<string> list = breakstring(str, 4, false);
??????????? IList<string> chslist = new List<string>();
??????????? //保留返回結果
??????????? string result = "";
??????????? //數字分節單位,四位為一組分節
??????????? //數字分節后的,段數目
??????????? int num = list.Count;
??????????? string temp;
??????????? //一段段全部轉化為漢字大寫數字
??????????? for (int i = 0; i < 6; i++)
??????????? {
??????????????? if (i > num-1) break;
??????????????? temp = list[i];
??????????????? temp = convertfournumber(temp);
??????????????? chslist.Add(temp);
??????????? }
??????????? //每一段補充數字單位,低于萬位不需要處理
??????????? //處理萬位段
??????????? if (chslist.Count > 1)
??????????? {
??????????????? if (chslist[1] != "")
??????????????????? chslist[1] = chslist[1] + "萬";
??????????? }
??????????? if (chslist.Count == 3)
??????????? {
??????????????? if (chslist[2] != "")
??????????????????? chslist[2] = chslist[2] + "億";
??????????? }
??????????? //處理億位和萬億位段,同時為空不需要處理
??????????? if (chslist.Count >= 4)
??????????? {
??????????????? if (chslist[2] != "" || chslist[3] != "")
??????????????? {
??????????????????? if (chslist[2] != "" && chslist[3] != "")
??????????????????? {
??????????????????????? chslist[2] = chslist[2] + "億";
??????????????????????? chslist[3] = chslist[3] + "萬";
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? if (chslist[3] != "")
??????????????????????????? chslist[3] = chslist[3] + "萬億";
??????????????????????? else
??????????????????????????? chslist[2] = chslist[2] + "億";
??????????????????? }
??????????????? }
??????????? }
??????????? if (chslist.Count == 5)
??????????? {
??????????????? if (chslist[4] != "")
??????????????????? chslist[4] = chslist[4] + "億億";
??????????? }
??????????? //處理億億位,萬億億位段,同時為空不需要處理
??????????? if (chslist.Count >= 6)
??????????? {
??????????????? if (chslist[4] != "" || chslist[5] != "")
??????????????? {
??????????????????? if (chslist[4] != "" && chslist[5] != "")
??????????????????? {
??????????????????????? chslist[4] = chslist[4] + "億億";
??????????????????????? chslist[5] = chslist[5] + "萬";
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? if (chslist[5] != "")
??????????????????????????? chslist[5] = chslist[5] + "萬億億";
??????????????????????? else
??????????????????????????? chslist[4] = chslist[4] + "億億";
??????????????????? }
??????????????? }
??????????? }
??????????? //補中間的零位,主要在有數字的段最前面補零,本節上面第一位為0,上節的最后一位有0,就需要補零
??????????? string temp1 ;
??????????? string temp2 ;
??????????? for (int i =0; i<chslist.Count-1; i++)
??????????? {
??????????????? temp = chslist[i];
??????????????? if (temp != "" )
??????????????? {
??????????????????? temp1 = list[i];
??????????????????? temp2 = list[i + 1];
??????????????????? if (temp1.Substring(0, 1) == "0"||temp2.Substring(temp2.Length-1,1)=="0")
??????????????????????? chslist[i] = "零" + chslist[i];
??????????????? }
??????????? }
??????????? //最后獲得結果
??????????? for (int i = 0; i < chslist.Count; i++)
??????????????? result = chslist[i] + result;
??????????? //除掉開始的'零'
??????????? if (result != "")
??????????? {
??????????????? if (result.Substring(0, 1) == "零")
??????????????????? result = result.Substring(1, result.Length - 1);
??????????? }
??????????? if (result == "")
??????????????? result = "零";
??????????? return result;
??????? }
??????? //小數部分轉化
??????? string decimalconvert(string str)
??????? {
??????????? if (!JudgeNumber(str))
??????????????? return "";
??????????? string result = "";
??????????? for (int i = 0; i < str.Length; i++)
??????????? {
??????????????? if (i == 0)
??????????????????? result = result +convertnumtochs( str.Substring(i, 1)) + "角";
??????????????? if(i==1)
??????????????????? result = result +convertnumtochs( str.Substring(i, 1)) + "分";
??????????????? if (i > 2)
??????????????????? break;
??????????? }
??????????? return result;
??????? }
??????? //整個數值轉化,阿拉伯金額轉換為漢字大寫金額
??????? string numconvert(string str)
??????? {
??????????? if(!JudgeNumber(str))
??????????????? return "";
??????????? int dotpos = str.IndexOf(".");
??????????? string intnum = "", decimalnum = "";
??????????? if (dotpos < 1)
??????????????? intnum = str;
??????????? else
??????????? {
??????????????? intnum = str.Substring(0, dotpos);
??????????????? decimalnum = str.Substring(dotpos + 1, str.Length - dotpos - 1);
??????????? }
??????????? if (intnum != "")
??????????????? intnum = intconvert(intnum);
???????????
??????????? if (decimalnum != "")
??????????????? decimalnum = decimalconvert(decimalnum);
??????????? if (intnum == "零" && decimalnum != "")
??????????????? intnum = "";
??????????? if (intnum != "")
??????????????? intnum = intnum + "元";
??????????? return intnum + decimalnum;
??????? }
總結
以上是生活随笔為你收集整理的人民币金额由阿拉伯数值转换成汉字大写数值的函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自动点击sdk,Andr
- 下一篇: MIUI——添加学校邮箱到电子邮件解决方