C#貨幣格式轉化執行個體解析

來源:互聯網
上載者:User

C#貨幣格式轉化執行個體向你介紹了一點在C#貨幣格式轉化時所用到的小技巧,希望通過這個執行個體使你瞭解C#貨幣格式轉化的使用細節,對你學習C#貨幣格式轉化方面有所協助。

C#貨幣格式轉化1.格式化輸入資料為貨幣格式

本執行個體主要用NumberFormatInfo類的CurrencyGroupSeparator屬性格式化輸入資料為貨幣格式。CurrencyGroupSeparator屬性擷取或設定在貨幣值中對小數點左邊數字進行分組的字串。運行程式,在輸入資料文字框中,輸入數字,單擊“格式化輸入資料為貨幣格式”按鈕,效果5.13所示。

C#貨幣格式應用主要代碼如下:

 
  1.  private void button1_Click(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5. NumberFormatInfo nfi = new CultureInfo("zh-CN", false).NumberFormat;  
  6.  
  7. nfi.CurrencyGroupSeparator = " ";  
  8.  
  9. textBox2.Text = Convert.ToDouble(textBox1.Text).ToString("c",nfi);  
  10.  
  11. }  

C#貨幣格式轉化2.如何將商品小寫金額轉化為大寫

本執行個體主要介紹如何將商品金額小寫轉換成大寫。運行程式,在文字框中輸入小寫金額,單擊【確定】按鈕執行轉換。主要代碼如下:

 
  1.  private void button1_Click(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5.   String[] Scale = { "分", "角", "元", "拾",   
  6. "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾",   
  7. "佰", "仟", "兆", "拾", "佰", "仟" };  
  8.  
  9. String[] Base = { "零", "壹", "貳", "三",  
  10.  "肆", "伍", "陸", "柒", "捌", "玖" };  
  11.  
  12. String Temp = textBox1.Text.ToString();  
  13.  
  14. String Info = null;  
  15.  
  16. int index = Temp.IndexOf(".",0,Temp.Length);//判斷是否有小數點  
  17.  
  18. if (index != -1)  
  19.  
  20. {  
  21.  
  22. Temp = Temp.Remove(Temp.IndexOf("."), 1);  
  23.  
  24. for (int i = Temp.Length; i > 0; i--)  
  25.  
  26. {  
  27.  
  28. int Data = Convert.ToInt16(Temp[Temp.Length - i]);  
  29.  
  30. Info += Base[Data - 48];  
  31.  
  32. Info += Scale[i - 1];  
  33.  
  34. }  
  35. }  
  36.  
  37. else 
  38.  
  39. {  
  40. for (int i = Temp.Length; i > 0; i--)  
  41.  
  42. {  
  43. int Data = Convert.ToInt16(Temp[Temp.Length - i]);  
  44.  
  45. Info += Base[Data - 48];  
  46.  
  47. Info += Scale[i+1];  
  48.  
  49. }  
  50. }  
  51. textBox2.Text = Info;  
  52.  
  53. }  

C#貨幣格式轉化執行個體的基本內容就向你介紹到這裡,希望對你瞭解和學習掌握C#貨幣格式轉化有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.