1. 程式人生 > >將輸入資料轉化為人民幣大寫輸出

將輸入資料轉化為人民幣大寫輸出

轉化為大寫的方法:

private string datachange(string str)
  {
   string read = "";
   switch (int.Parse(str))
   {
    case 0:
     read = "零";
     break;
    case 1:
     read = "壹";
     break;
    case 2:
     read = "貳";
     break;
    case 3:
     read = "叄";
     break;
    case 4:
     read = "肆";
     break;
    case 5:
     read = "伍";
     break;
    case 6:
     read = "陸";
     break;
    case 7:
     read = "柒";
     break;
    case 8:
     read = "捌";
     break;
    case 9:
     read = "玖";
     break;
   }
   return read;
  }

新增單位的方法:

private string china(int i)
  {
   string read1 = "";
   switch (i)
   {
    case 8:
     read1 = "億";
     break;
    case 7:
     read1 = "仟";
     break;
    case 6:
     read1 = "佰";
     break;
    case 5:
     read1 = "拾";
     break;
    case 4:
     read1 = "萬";
     break;
    case 3:
     read1 = "仟";
     break;
    case 2:
     read1 = "佰";
     break;
    case 1:
     read1 = "拾";
     break;
    case 0:
     read1 = "元";
     break;
   }
   return read1;
  }

轉化按鈕事件:

private void Button1_Click(object sender, System.EventArgs e)
  {
   int length = 0;
   string print = "";
   string str = "";
   string ss ="";
   if(Regex.IsMatch(TextBox1.Text,@"^(/d*/./d*|/d*)$"))
   {
    ss = double.Parse(TextBox1.Text).ToString("#0.00");
    length = ss.Length-3;
    str = datachange(ss.Substring(ss.Length-2,1))+"角"+datachange(ss.Substring(ss.Length-1,1))+"分";
   }
   else
   {
    Response.Write("輸入格式不正確!");
   }
   for(int i=0;i<length;i++)
   {
    print += datachange(ss.Substring(i,1))+china(length-(i+1));
   }
   Response.Write(print+str);
  }