String.Format

String.Format

//一般用法

//小數換算



string str3 = String.Format("{0:F2}", 0.125); //result: 0.13

string str4 = String.Format("{0:F3}", 0.125); //result: 0.125

string str5 = String.Format("{0:N1}", 123456.7891); //result: 123,456.8 四捨五入,N後面的數字表示保留幾個小數位數

string str6 = String.Format("{0:N2}", 123456.7891); //result: 123,456.79

string str7 = String.Format("{0:N3}", 123456.7891); //result: 123,456.789

decimal d = decimal.Round(decimal.Parse("0.55555"), 2); //result: 0.56 四捨五入

double d = Math.Round(0.55555, 2); //result: 0.56 四捨五入

double d = 0.55555;

string str8 = d.ToString("f2");//fN 保留N位,四捨五入

//小數點後面幾個0就保留幾個小數位數



string str9 = d.ToString("#0.00");

string str10 = (56789 / 100.0).ToString("#.##"); //result: 567.89

string str11 = (56789 / 100).ToString("#.##"); //result: 567

double num1 = 123456.123456;

string str = num1.ToString("#0.####");//幾個#等於顯示幾個小數位數且四捨五入

//output: 123456.1235