C#小數(shù)點(diǎn)格式化用法小結(jié)
本文實(shí)例講述了C#小數(shù)點(diǎn)格式化用法。分享給大家供大家參考,具體如下:
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.(補(bǔ)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.
"四舍六入五考慮,五后非零就進(jìn)一,五后皆零看奇偶,五前為偶應(yīng)舍 去,五前為奇要進(jìn)一"
短一點(diǎn)的口訣叫“四舍、六入、五湊偶”
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;//小數(shù)點(diǎn)保留幾位數(shù).
provider.PercentPositivePattern = 1;//百分號出現(xiàn)在何處.
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
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
在多線程中調(diào)用winform窗體控件的實(shí)現(xiàn)方法
這篇文章主要介紹了在多線程中調(diào)用winform窗體控件的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)圖片放大功能的按照像素放大圖像方法
這篇文章主要介紹了C#實(shí)現(xiàn)圖片放大功能的按照像素放大圖像方法,功能非常實(shí)用,需要的朋友可以參考下2014-07-07
C#操作NPOI實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了C#如何操作NPOI實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法
這篇文章主要介紹了C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法,涉及C#操作系統(tǒng)進(jìn)程的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

