C#开发经验技巧宝典5.docx
《C#开发经验技巧宝典5.docx》由会员分享,可在线阅读,更多相关《C#开发经验技巧宝典5.docx(57页珍藏版)》请在冰豆网上搜索。
C#开发经验技巧宝典5
5.1 数字处理技巧
本实例主要介绍如何对计算结果四舍五入。
本实例主要是通过Math类的Pow方法来实现的。
运行程序,在文本框中输入数字,单击“确定”按钮四舍五入文本框中的数字。
主要代码如下:
publicstaticdoubleRound(doubled,inti)
{
if(d>=0)
{
d+=5*Math.Pow(10,-(i+1));//
}
else
{
d+=-5*Math.Pow(10,-(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);//截取需要位数
}
if(poststr.Length<=2)
{
poststr=poststr+"0";
}
stringstrd=prestr+"."+poststr;
d=Double.Parse(strd);//将字符串转换为双精度实数
returnd;
}
参数d表示要四舍五入的数;i表示要保留的小数点后的位数。
本实例主要介绍如何将商品金额小写转换成大写。
运行程序,在文本框中输入小写金额,单击【确定】按钮执行转换。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
String[]Scale={"分","角","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","兆","拾","佰","仟"};
String[]Base={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
StringTemp=textBox1.Text.ToString();
StringInfo=null;
intindex=Temp.IndexOf(".",0,Temp.Length);//判断是否有小数点
if(index!
=-1)
{
Temp=Temp.Remove(Temp.IndexOf("."),1);
for(inti=Temp.Length;i>0;i--)
{
intData=Convert.ToInt16(Temp[Temp.Length-i]);
Info+=Base[Data-48];
Info+=Scale[i-1];
}
}
else
{
for(inti=Temp.Length;i>0;i--)
{
intData=Convert.ToInt16(Temp[Temp.Length-i]);
Info+=Base[Data-48];
Info+=Scale[i+1];
}
}
textBox2.Text=Info;
}
本实例主要介绍如何根据生日自动计算员工年龄。
通常的方法是,先从员工的生日字符中取出年份,然后用当前的日期年份减去员工出生年份,得到的整数就是员工实际年龄。
主要代码如下:
string m_Str = "1984-04-04";
int m_Y1 = DateTime.Parse(m_Str).Year;
int m_Y2 = DateTime.Now.Year;
intm_Age=m_Y2-m_Y1;
本实例主要介绍如何设置货币数值中使用的小数位数。
主要是使用NumberFormatInfo类的CurrencyDecimalDigits属性来设置在货币值中使用的小数位数,使用CultureInfo类的NumberFormat属性来指定字符串的域区样。
语法格式如下:
publicintCurrencyDecimalDigits{get;set;}
属性值:
要在货币值中使用的小数位数。
InvariantInfo的默认值为2。
CultureInfo类提供有关特定区域性的信息(如区域性的名称、书写系统和使用的日历)以及如何设置日期和排序字符串的格式。
此类有多种重载形式,本实例使用的重载形式如下:
publicCultureInfo(intculture,booluseUserOverride)
参数说明
l culture:
预定义的CultureInfo标识符、现有CultureInfo对象的LCID属性或仅Windows区域性标识符。
l UseUserOverride:
一个布尔值,指示是使用用户选定的区域性设置(True),还是使用默认区域性设置(False)。
NumberFormat属性获取或设置NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。
其语法格式如下:
publicvirtualNumberFormatInfoNumberFormat{get;set;}
属性值:
一个NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。
主要代码如下:
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
Int64myInt=12345;
privatevoidbutton2_Click(objectsender,EventArgse)
{
GN.CurrencyDecimalDigits=2;
MessageBox.Show(myInt.ToString("C",GN),"保留两位小数");
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
GN.CurrencyDecimalDigits=3;
MessageBox.Show(myInt.ToString("C",GN),"保留三位小数");
}
本实例主要介绍如何自定义货币值中的小数点,主要是使用NumberFormatInfo类的CurrencyDecimalSeparator属性来完成的。
运行程序,效果如图5.1和图5.2所示。
图5.1 自定义$符 图5.2 自定义*符
CurrencyDecimalSeparator属性获取或设置要在货币值中用做小数点分隔符的字符串。
其语法格式如下:
publicstringCurrencyDecimalSeparator{get;set;}
属性值:
要在货币值中用做小数点分隔符的字符串,默认值为“.”。
主要代码如下:
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
Int64myInt=123456789;
privatevoidbutton1_Click(objectsender,EventArgse)
{
GN.CurrencyDecimalSeparator="$";
MessageBox.Show("定义前:
"+myInt.ToString("C")+"\n"+"定义后:
"+myInt.ToString("C",GN),"自定义小数点为$符");
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
GN.CurrencyDecimalSeparator="*";
MessageBox.Show("定义前:
"+myInt.ToString("C")+"\n"+"定义后:
"+myInt.ToString("C",GN),"自定义小数点为*符");
}
本实例主要介绍如何自定义货币值中对小数点左边数字分组字符,主要是使用NumberFormatInfo类的CurrencyGroupSeparator属性来设置的。
运行程序,其效果如图5.3所示。
CurrencyGroupSeparator属性获取或设置在货币值中对小数点左边数字进行分组的字符串。
其语法格式如下:
publicstringCurrencyGroupSeparator{get;set;}
属性值:
在货币值中对小数点左边数字进行分组的字符串,默认值为“,”。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("en-US",false).NumberFormat;
Int64myInt=123456789;
GN.CurrencyGroupSeparator="、";
MessageBox.Show("定义前:
"+myInt.ToString("C")+"\n"+"定义后:
"+myInt.ToString("C",GN),"分组字符用、号");
}
本实例主要介绍如何自定义货币值中的小数点,主要是使用NumberFormatInfo类的CurrencyGroupSizes属性来完成的。
运行程序,效果如图5.4和图5.5所示。
图5.4 {2,3,1}格式 图5.5 {2,3,2}格式
CurrencyGroupSizes属性获取或设置货币值中小数点左边每一组的位数。
其语法格式如下:
publicint[]CurrencyGroupSizes{get;set;}
属性值:
货币值中小数点左边每一组的位数,默认值是一个一维数组,该数组只包含一个设置为3的元素。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoCN=newSystem.Globalization.CultureInfo("en-US",false).NumberFormat;
Int64myInt=123456789012345;
int[]mySizes1={2,3,1};
int[]mySizes2={2,3,2};
CN.CurrencyGroupSizes=mySizes1;
MessageBox.Show("定义前:
"+myInt.ToString("C")+"\n"+"定义后:
"+myInt.ToString("C",CN),"{2,3,1}格式");
CN.CurrencyGroupSizes=mySizes2;
MessageBox.Show("定义前:
"+myInt.ToString("C")+"\n"+"定义后:
"+myInt.ToString("C",CN),"{2,3,2}格式");
}
本实例主要介绍如何使用NumberFormatInfo类的PercentDecimalDigits属性来设置百分比值中小数点后面保留的位数。
运行程序,效果如图5.6所示。
PercentDecimalDigits属性获取或设置在百分比值中使用的小数位数。
其语法格式如下:
publicintPercentDecimalDigits{get;set;}
属性值:
在百分比值中使用的小数位数,默认值为2。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
DoublemyInt=0.9774;
stringstrInfo=null;
GN.PercentDecimalDigits=1;
strInfo+="保留一位小数:
"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=2;
strInfo+="\n保留二位小数:
"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=4;
strInfo+="\n保留四位小数:
"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=6;
strInfo+="\n保留六位小数:
"+string.Format(myInt.ToString("P",GN));
MessageBox.Show(strInfo,"定义效果");
}
本实例主要介绍如何使用NumberFormatInfo类的PercentDecimalSeparator属性设置百分比小数点分隔符的字符串。
运行程序,效果如图5.7所示。
PercentDecimalSeparator属性设置在百分比值中用做小数点分隔符的字符串。
其语法格式如下:
publicstringPercentDecimalSeparator{get;set;}
属性值:
在百分比值中用做小数点分隔符的字符串,默认值为“.”。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
DoublemyInt=0.122434;
stringstrPercent=null;
GN.PercentDecimalSeparator="";
strPercent+="用空格代替小数点:
"+myInt.ToString("P",GN);
GN.PercentDecimalSeparator="@";
strPercent+="\n用@符代替小数点:
"+myInt.ToString("P",GN);
GN.PercentDecimalSeparator="#";
strPercent+="\n用#符代替小数点:
"+myInt.ToString("P",GN);
GN.PercentDecimalSeparator="*";
strPercent+="\n用*号代替小数点:
"+myInt.ToString("P",GN);
MessageBox.Show(strPercent,"定义效果");
}
本实例主要介绍如何使用NumberFormatInfo类的PercentGroupSeparator属性设置百分比小数点左边的分节号。
运行程序,效果如图5.8所示。
PercentGroupSeparator属性获取或设置在百分比值中对小数点左边数字进行分组的字符串。
其语法格式如下:
publicstringPercentGroupSeparator{get;set;}
属性值:
在百分比值中对小数点左边数字进行分组的字符串,默认值为“,”。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
DoublemyInt=12345.678;
stringstrSperar=null;
GN.PercentGroupSeparator=",";
strSperar+="用逗号作分隔符:
"+myInt.ToString("P",GN);
GN.PercentGroupSeparator="、";
strSperar+="\n用顿号作分隔符:
"+myInt.ToString("P",GN);
GN.PercentGroupSeparator="'";
strSperar+="\n用单引号作分隔符:
"+myInt.ToString("P",GN);
MessageBox.Show(strSperar,"定义效果");
}
本实例主要介绍如何使用NumberFormatInfo类的PercentGroupSizes属性设置百分比小数点左边每一组的位数。
运行程序,效果如图5.9所示。
PercentGroupSizes属性获取或设置百分比值中小数点左边的每一组的位数。
其语法格式如下:
publicint[]PercentGroupSizes{get;set;}
属性值:
百分比值中小数点左边的每一组的位数。
默认值是一个一维数组,该数组只包含一个设置为3的元素。
主要代码如下:
privatevoidbutton1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=newSystem.Globalization.CultureInfo("zh-CN",false).NumberFormat;
DoublemyInt=510214720805231.4835;
stringstrInt=null;
strInt+="默认格式:
"+myInt.ToString("p",GN);
int[]MySizes1={2,3,4};
GN.PercentGr