1.system.globalization.numberformatinfo Provider = new System.Globalization.NumberFormatInfo ();
Provider. Numberdecimaldigits =intdeclength; Number of decimal places to set
Double strcashamt=convert.todouble (This.txtCashAmt.Text); First, turn the values in the controls into double
This.txtCashAmt.Text = strcashamt.tostring ("N", provider); Using the ToString function to format decimal places
2. Keep n bit, rounded.
Decimal d= Decimal. Round (decimal. Parse ("0.55555"), 2);
3. Keep N bit rounded
Math.Round (0.55555,2)
4, keep n bit rounded
Double dbdata = 0.55555;
String str1 = Dbdata. ToString ("F2");//fn keep n bit, rounded
5. Keep N bit rounded
string result = String.Format ("{0:n2}", 0.55555);//2 bit
string result = String.Format ("{0:n3}", 0.55555);//3 bits
6. Keep n-bit rounded (yes)
Double s=0.55555;
Result=s.tostring ("#0.00"); a few 0 on the back of the point keep a few
In C #, if you show how to keep the decimal digits, and the percent-semicolon solution:
1, with the NumberFormatInfo class to solve:
System.Globalization.NumberFormatInfo Provider = new System.Globalization.NumberFormatInfo ();
Provider. Percentdecimaldigits = 2;//A decimal point to retain several digits.
Provider. Percentpositivepattern = where 2;//percent signs appear.
Double result = (double) 1/3;//Be sure to use double type.
Response.Write (Result. ToString ("P", provider));
2, with the ToString method.:
public string getrate (double Hcount, double task)
{
String RValue;
String temp = "";
if (task = 0)
{
task = 1;
}
Double db = (hcount/task) * 100;
if (hcount >= Task)
{
rValue = "100%";
}
Else
{
rValue = db. ToString ("#0. #0") + "%";
}
return rValue;
}
String str1 = String.Format ("{0:n1}", 56789);//result:56,789.0
String str2 = String.Format ("{0:n2}", 56789); result:56,789.00
String str3 = String.Format ("{0:n3}", 56789);//result:56,789.000
String str8 = String.Format (" {0:F1} ", 56789); result:56789.0
String STR9 = String.Format ("{0:f2}", 56789);//result:56789.00
String str11 = (56789/100.0). ToString ("#.##"); result:567.89
String str12 = (56789/100). ToString ("#.##"); result:567