C#小數點格局化用法小結。本站提示廣大學習愛好者:(C#小數點格局化用法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#小數點格局化用法小結正文
本文實例講述了C#小數點格局化用法。分享給年夜家供年夜家參考,詳細以下:
1.ToString()辦法
double d=12345678.2334; Console.WriteLine(d.ToString("F2")); //1234.23 Console.WriteLine(d.ToString("###,###.00")); //12,345,678.23
2.Math.Round()辦法
Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4. Math.Round(3.46, 1); //Returns 3.5. Math.Round(3.445, 1); //Returns 3.4. Math.Round(3.455, 1); //Returns 3.5. Math.Round(3.465, 1); //Returns 3.5. Math.Round(3.450, 1); //Returns 3.4.(補0是有效的) Math.Round(3.4452, 2); //Returns 3.45. Math.Round(3.4552, 2); //Returns 3.46. Math.Round(3.4652, 2); //Returns 3.47.
"四捨六入五斟酌,五後非零就進一,五後皆零看奇偶,五前為偶應捨 去,五前為奇要進一"
短一點的口訣叫“四捨、六入、五湊偶”
3.double.Parse()辦法
double d=1.12345; d=double.Parse(d.ToString("0.00")); //1.12
4.輸入百分號
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); provider.PercentDecimalDigits = 2;//小數點保存幾位數. provider.PercentPositivePattern = 1;//百分號湧現在何處. double result = (double)1 / 3;//必定要用double類型. Console.WriteLine(result.ToString("P", provider)); //33.33% //或 Console.WriteLine((result*100).ToString("#0.#0")+"%");
5.String.Format()辦法
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
願望本文所述對年夜家C#法式設計有所贊助。