C # tostring Formatting

Source: Internet
Author: User

The default ToString () output for data fetched from the database and locally generated data may not always be used when doing a custom alignment of a class object, as follows:

// 2018-01-29 // sql--date--2018-01-29 00:00:00.123 // 2018-01-29 00:00:00 // 1m // sql--numeric (2)--1.00 // 1

The number is converted to string, and the number of decimal digits is reserved using Math.Round:

 Public voidconverttostring () {//float--f    varA =1f; Console.WriteLine (Math.Round (A,4));//1A =1.12f; Console.WriteLine (Math.Round (A,4));//1.12A =1.12345f; Console.WriteLine (Math.Round (A,4));//1.1235//decimal--m,double--d    varb = 1m;//1d;Console.WriteLine (Math.Round (b,4));//1b =1.12m;//1/12d;Console.WriteLine (Math.Round (b,4));//1.12b =1.12345m;//1.12345dConsole.WriteLine (Math.Round (b,4));//1.1234}

The first problem is the decimal point of the number itself, and the second problem is the algorithm for rounding small numbers.

Consider using the tostring format string, organized as follows:

1.Guid

varGUID =Guid.NewGuid ();//d,n,b,p Uppercase lowercase effects are the sameConsole.WriteLine (GUID. ToString ("D"));//10244798-9a34-4245-b1ef-9143f9b1e68aConsole.WriteLine (GUID. ToString ("N"));//102447989a344245b1ef9143f9b1e68aConsole.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 =NewDateTime (2018,1,1, A,5,5,123);//2018-01-01 05:06:07.123Console.WriteLine (dt. ToString ("y"));//January 2018Console.WriteLine (dt. ToString ("Y"));//January 2018Console.WriteLine (dt. ToString ("m"));//January 1Console.WriteLine (dt. ToString ("M"));//January 1Console.WriteLine (dt. ToString ("D"));//2018-01-01Console.WriteLine (dt. ToString ("D"));//January 1, 2018Console.WriteLine (dt. ToString ("T"));//12:05Console.WriteLine (dt. ToString ("T"));//12:05:05Console.WriteLine (dt. ToString ("%d"));//11 Month One dayConsole.WriteLine (dt. ToString ("%h"));//one hour in 121 daysConsole.WriteLine (dt. ToString ("%H"));//one hour in 121 daysConsole.WriteLine (dt. ToString ("DD"));//11 Month One dayConsole.WriteLine (dt. ToString ("DDD"));//one day, one by one weeks in a week, defined in Abbreviateddaynames. Console.WriteLine (dt. ToString ("dddd"));//Day of the week one by one weeks, defined in DayNamesConsole.WriteLine (dt. ToString ("MM"));//one month in 11 yearsConsole.WriteLine (dt. ToString ("MMM"));//abbreviated name for the month of January, defined in AbbreviatedMonthNames. Console.WriteLine (dt. ToString ("MMMM"));//The full name of the month of January, defined in MonthNames. Console.WriteLine (dt. ToString ("yy"));// -Console.WriteLine (dt. ToString ("yyyy"));//2018Console.WriteLine (dt. ToString ("GG"));//A.D.Console.WriteLine (dt. ToString ("hh"));//12 12-hour systemConsole.WriteLine (dt. ToString ("HH"));//12 24-hour systemConsole.WriteLine (dt. ToString ("TT"));//afternoon morning, afternoonConsole.WriteLine (dt. ToString ("mm"));// to

3.Number

//C CurrencyConsole.WriteLine (2.5. ToString ("C"));//¥2.50Console.WriteLine (2.5. ToString ("C5"));//¥2.50000//D decimal NumberConsole.WriteLine ( -. ToString ("D5"));//00025, not enough to fill the front 0//E-ScientificConsole.WriteLine (25000. ToString ("E"));//2.500000E+004//F fixed pointConsole.WriteLine ( -. ToString ("F2"));//25.00//G GeneralConsole.WriteLine (2.5. ToString ("G"));//2.5//N NumberConsole.WriteLine (2500000. ToString ("N"));//2,500,000.00//X HexConsole.WriteLine ( the. ToString ("X"));// -//#.00 the number of decimal digits reservedConsole.WriteLine ( the. ToString ("#.00"));//256.00//#,#.00 thousand separatorsConsole.WriteLine (25656. ToString ("#,#.00"));//256.00

C # tostring Formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.