This tip describes how to use the Percentdecimaldigits property of the NumberFormatInfo class to set the number of digits reserved after the decimal point in the percent value.
Percentdecimaldigits Property
Use this property to get or set the number of decimal places to use in percent values.
The syntax format is:
public int PercentDecimalDigits{get;set;}
Property value:-The number of decimal places to use in percent values. The default value is 2.
The main code is as follows:
private void button1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=new System.Globalization.CultureInfo("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,"定义效果");
}