資源描述:
《c#開發(fā)經(jīng)驗(yàn)技巧寶典》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、5.1?數(shù)字處理技巧本實(shí)例主要介紹如何對計(jì)算結(jié)果四舍五入。本實(shí)例主要是通過Math類的Pow方法來實(shí)現(xiàn)的。運(yùn)行程序,在文本框中輸入數(shù)字,單擊“確定”按鈕四舍五入文本框中的數(shù)字。主要代碼如下:?????publicstaticdoubleRound(doubled,inti)???????{???????????if(d>=0)???????????{???????????????d+=5*Math.Pow(10,-(i+1));//???????????}???????????else???????????{???????????????d+=-5*Math.Pow(10,-
2、(i+1));???????????}???????????stringstr=d.ToString();???????????string[]strs=str.Split('.');???????????intidot=str.IndexOf('.');???????????stringprestr=strs[0];???????????stringpoststr=strs[1];???????????if(poststr.Length>i)???????????{???????????????poststr=str.Substring(idot+1,i);//截取需要位數(shù)
3、???????????}???????????if(poststr.Length<=2)???????????{???????????????poststr=poststr+"0";???????????}???????????stringstrd=prestr+"."+poststr;???????????d=Double.Parse(strd);//將字符串轉(zhuǎn)換為雙精度實(shí)數(shù)???????????returnd;???????}參數(shù)d表示要四舍五入的數(shù);i表示要保留的小數(shù)點(diǎn)后的位數(shù)。本實(shí)例主要介紹如何將商品金額小寫轉(zhuǎn)換成大寫。運(yùn)行程序,在文本框中輸入小寫金額,單擊【確定】按
4、鈕執(zhí)行轉(zhuǎn)換。主要代碼如下:????privatevoidbutton1_Click(objectsender,EventArgse)???????{?String[]Scale={"分","角","元","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","兆","拾","佰","仟"};???????????String[]Base={"零","壹","貳","叁","肆","伍","陸","柒","捌","玖"};???????????StringTemp=textBox1.Text.ToString();???????????Stri
5、ngInfo=null;???????????intindex=Temp.IndexOf(".",0,Temp.Length);//判斷是否有小數(shù)點(diǎn)???????????if(index!=-1)???????????{???????????????Temp=Temp.Remove(Temp.IndexOf("."),1);???????????????for(inti=Temp.Length;i>0;i--)???????????????{???????????????????intData=Convert.ToInt16(Temp[Temp.Length-i]);????
6、???????????????Info+=Base[Data-48];???????????????????Info+=Scale[i-1];???????????????}???????????}???????????else???????????{???????????????for(inti=Temp.Length;i>0;i--)???????????????{????????????????????????????????????intData=Convert.ToInt16(Temp[Temp.Length-i]);???????????????????Info+
7、=Base[Data-48];???????????????????Info+=Scale[i+1];???????????????}?????????????????????????}???????????textBox2.Text=Info;???????}本實(shí)例主要介紹如何根據(jù)生日自動計(jì)算員工年齡。通常的方法是,先從員工的生日字符中取出年份,然后用當(dāng)前的日期年份減去員工出生年份,得到的整數(shù)就是員工實(shí)際年齡。主要代碼如下:?????????????string??m_Str??=??"1984-04