在做類對象自定義比對的時候,從數據庫中取出的數據和本地生成的數據使用默認的ToString()輸出未必一直,如下:
//2018-01-29 //sql--date--2018-01-29 00:00:00.123 //2018-01-29 00:00:00 //1m //sql--numeric(18, 2)--1.00 //1
數字轉換為String,使用Math.Round保留小數位數:
public void ConvertToString() { //float--f var a = 1f; Console.WriteLine(Math.Round(a, 4));//1 a = 1.12f; Console.WriteLine(Math.Round(a, 4));//1.12 a = 1.12345f; Console.WriteLine(Math.Round(a, 4));//1.1235 //decimal--m,double--d var b = 1m;//1d; Console.WriteLine(Math.Round(b, 4));//1 b = 1.12m;//1/12d; Console.WriteLine(Math.Round(b, 4));//1.12 b = 1.12345m;//1.12345d Console.WriteLine(Math.Round(b, 4));//1.1234,不是四舍五入,而是四舍六入五取偶 }
第一個問題就是數字本身小數點,第二個問題就是小數值四舍五入的算法,微軟的Math.Round並不是傳統的中國四舍五入,而是四舍六入五取偶,即0.1235==0.124,0.1245==0.124。
Math.Round有8中重載,如下:
Round(Decimal) Round(Double) Round(Decimal, Int32) Round(Decimal, MidpointRounding) Round(Double, Int32) Round(Double, MidpointRounding) Round(Decimal, Int32, MidpointRounding)//僅在這設置為MidpointRounding.AwayFromZero,能保證是四舍五入 Round(Double, Int32, MidpointRounding)
只有在Round(Decimal, Int32, MidpointRounding=MidpointRounding.AwayFromZero)時是四舍五入。
如下測試:
using System; public class Test { public static void Main() { Console.WriteLine(Math.Round(0.31249999m,3,MidpointRounding.AwayFromZero));//0.312 Console.WriteLine(Math.Round(0.31249999,3,MidpointRounding.AwayFromZero));//0.312
Console.WriteLine(Math.Round(0.31249999f,3,MidpointRounding.AwayFromZero));//0.313 Console.ReadKey(); } }
考慮使用ToString格式化字符串,整理如下:
1.Guid
var guid = Guid.NewGuid(); //D,N,B,P大寫小寫效果是一樣 Console.WriteLine(guid.ToString("D"));// 10244798-9a34-4245-b1ef-9143f9b1e68a Console.WriteLine(guid.ToString("N"));// 102447989a344245b1ef9143f9b1e68a Console.WriteLine(guid.ToString("B"));// {10244798-9a34-4245-b1ef-9143f9b1e68a} Console.WriteLine(guid.ToString("P"));// (10244798-9a34-4245-b1ef-9143f9b1e68a) Console.WriteLine(guid.ToString());// 10244798-9a34-4245-b1ef-9143f9b1e68a
2.Date
DateTime dt = new DateTime(2018, 1, 1, 12, 5, 5, 123);//2018-01-01 05:06:07.123 Console.WriteLine(dt.ToString("y"));//2018年1月 Console.WriteLine(dt.ToString("Y"));//2018年1月 Console.WriteLine(dt.ToString("m"));//1月1日 Console.WriteLine(dt.ToString("M"));//1月1日 Console.WriteLine(dt.ToString("d"));//2018-01-01 Console.WriteLine(dt.ToString("D"));//2018年1月1日 Console.WriteLine(dt.ToString("t"));//12:05 Console.WriteLine(dt.ToString("T"));//12:05:05 Console.WriteLine(dt.ToString("%d"));//1 一月中某天 Console.WriteLine(dt.ToString("%h"));//12 一天中某小時 Console.WriteLine(dt.ToString("%H"));//12 一天中某小時 Console.WriteLine(dt.ToString("dd"));//01 一月中某天 Console.WriteLine(dt.ToString("ddd"));//周一 一周中某一天,在AbbreviatedDayNames中定義。 Console.WriteLine(dt.ToString("dddd"));//星期一 一星期中某一天,在DayNames中定義 Console.WriteLine(dt.ToString("MM"));//01 一年中某月 Console.WriteLine(dt.ToString("MMM"));//一月 月份的縮寫名稱,在AbbreviatedMonthNames中定義。 Console.WriteLine(dt.ToString("MMMM"));//一月 月份的完整名稱,在MonthNames中定義。 Console.WriteLine(dt.ToString("yy"));//18 Console.WriteLine(dt.ToString("yyyy"));//2018 Console.WriteLine(dt.ToString("gg"));//公元 Console.WriteLine(dt.ToString("hh"));//12 12小時制 Console.WriteLine(dt.ToString("HH"));//12 24小時制 Console.WriteLine(dt.ToString("tt"));//下午 上午,下午 Console.WriteLine(dt.ToString("mm"));//05
3.Number
//C 貨幣 Console.WriteLine(2.5.ToString("C"));//¥2.50 Console.WriteLine(2.5.ToString("C5"));//¥2.50000 //D 十進制數 Console.WriteLine(25.ToString("D5"));//00025,不夠前補0 //E 科學型 Console.WriteLine(25000.ToString("E"));//2.500000E+004 //F 固定點 Console.WriteLine(25.ToString("F2"));//25.00 //G 常規 Console.WriteLine(2.5.ToString("G"));//2.5 //N 數字 Console.WriteLine(2500000.ToString("N"));//2,500,000.00 //X 十六進制 Console.WriteLine(256.ToString("X"));//100 //#.00 保留小數位數 Console.WriteLine(256.ToString("#.00"));//256.00 //#,#.00 千位分隔符 Console.WriteLine(25656.ToString("#,#.00"));//256.00 //固定位數(正數部分以及小數部分都是固定位數),不足補零,0是補位,有數字則占位,沒則補零 Console.WriteLine(3.5.ToString("0000.00"));//0003.50 Console.WriteLine(30000.5.ToString("0,000.00"));//30,003.50 Console.WriteLine(30000000.5.ToString("0,000.00"));//30,000,000.50