C#貨幣格式轉(zhuǎn)化實(shí)例解析
C#貨幣格式轉(zhuǎn)化實(shí)例向你介紹了一點(diǎn)在C#貨幣格式轉(zhuǎn)化時(shí)所用到的小技巧,希望通過這個(gè)實(shí)例使你了解C#貨幣格式轉(zhuǎn)化的使用細(xì)節(jié),對你學(xué)習(xí)C#貨幣格式轉(zhuǎn)化方面有所幫助。
C#貨幣格式轉(zhuǎn)化1.格式化輸入數(shù)據(jù)為貨幣格式
本實(shí)例主要用NumberFormatInfo類的CurrencyGroupSeparator屬性格式化輸入數(shù)據(jù)為貨幣格式。CurrencyGroupSeparator屬性獲取或設(shè)置在貨幣值中對小數(shù)點(diǎn)左邊數(shù)字進(jìn)行分組的字符串。運(yùn)行程序,在輸入數(shù)據(jù)文本框中,輸入數(shù)字,單擊“格式化輸入數(shù)據(jù)為貨幣格式”按鈕,效果如圖5.13所示。
C#貨幣格式應(yīng)用主要代碼如下:
- private void button1_Click(object sender, EventArgs e)
- {
- NumberFormatInfo nfi = new CultureInfo("zh-CN", false).NumberFormat;
- nfi.CurrencyGroupSeparator = " ";
- textBox2.Text = Convert.ToDouble(textBox1.Text).ToString("c",nfi);
- }
C#貨幣格式轉(zhuǎn)化2.如何將商品小寫金額轉(zhuǎn)化為大寫
本實(shí)例主要介紹如何將商品金額小寫轉(zhuǎn)換成大寫。運(yùn)行程序,在文本框中輸入小寫金額,單擊【確定】按鈕執(zhí)行轉(zhuǎn)換。主要代碼如下:
- private void button1_Click(object sender, EventArgs e)
- {
- String[] Scale = { "分", "角", "元", "拾",
- "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾",
- "佰", "仟", "兆", "拾", "佰", "仟" };
- String[] Base = { "零", "壹", "貳", "叁",
- "肆", "伍", "陸", "柒", "捌", "玖" };
- String Temp = textBox1.Text.ToString();
- String Info = null;
- int index = Temp.IndexOf(".",0,Temp.Length);//判斷是否有小數(shù)點(diǎn)
- if (index != -1)
- {
- Temp = Temp.Remove(Temp.IndexOf("."), 1);
- for (int i = Temp.Length; i > 0; i--)
- {
- int Data = Convert.ToInt16(Temp[Temp.Length - i]);
- Info += Base[Data - 48];
- Info += Scale[i - 1];
- }
- }
- else
- {
- for (int i = Temp.Length; i > 0; i--)
- {
- int Data = Convert.ToInt16(Temp[Temp.Length - i]);
- Info += Base[Data - 48];
- Info += Scale[i+1];
- }
- }
- textBox2.Text = Info;
- }
C#貨幣格式轉(zhuǎn)化實(shí)例的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)掌握C#貨幣格式轉(zhuǎn)化有所幫助。
【編輯推薦】