System.Globalization.NumberFormatInfo GN = new
System.Globalization.Cultu
reInfo("zh-CN", false).NumberFormat; Double myInt = 0.9774; string strInfo=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属性设置在百分比值中用做小数点分隔符的字符串。其语法格式如下:
public string PercentDecimalSeparator { get; set; }
属性值:在百分比值中用做小数点分隔符的字符串,默认值为“.”。 主要代码如下:
private void button1_Click(object sender, EventArgs e) {
System.Globalization.NumberFormatInfo GN = new System.Globalization.CultureInfo("zh-CN", false).NumberFormat;
Double myInt = 0.122434;