[Arrangement] C # tostring Format String arrangement (Format) (Standard Format setting specifiers for numbers, dates, and enumerations) (samwang)

Source: Internet
Author: User

In daily development, there are many situations of format strings. I often forget it, and it is a little difficult to check it. So today I will spend some time organizing it.

The format string is formatted by numbers, dates, and enumerations.

 

I. digit Format String
C or C Local currency format
D Or d Converts an integer to a base value of 10 in decimal format. If a precision specifier is given, the leading 0 is added.
E or E In scientific notation (INDEX) format, the number of decimal places (6 by default) is set for the precision specifier, and the case (E or E) of the format string determines the case of the index symbol.
F or F Fixed Point format. The number of decimal places can be set to 0.
G or G The general format. The E or F format depends on which format is relatively simple.
N or N Number Format. Use commas (,) to represent a kilobytes of characters, such as 32,767.44.
P or P Percent format
X or X Hexadecimal format, with the precision specifier used to add leading 0

  

First, use an example to describe how to format strings:

  

      double d = 123.456;     Console.WriteLine("ToString:{0}", d.ToString("C"));     Console.WriteLine("Format:{0}", string.Format("{0:C}",d));     Console.WriteLine("Console:{0:C}", d);

 

Output result:

  

  

Example of a number formatting program:

Console. writeline ("hexadecimal format character X: {0}", (145 ). tostring ("X"); // X only supports integer double [] numbers = {1054.32179,-195489100.8377, 1.0437e21,-1.0573e-05 }; string [] specifiers = {"C", "E", "F", "g", "n", "P", "r", "#, 000.000 ", "0. ### E-000 "," 000,000,000,000.00 ### "}; foreach (double number in numbers) {console. writeline ("formatting of {0}:", number); foreach (string specifier in specifiers) {console. writeline ("{0, 5 }:{ 1}", specifier, number. tostring (specifier);} console. writeline ();}

Output result:

  

Msdn: Double. tostring method (string)

 

Ii. Date Format String

  

        static void DateToString()        {            DateTime dateValue = DateTime.Now;            // Create an array of standard format strings.            string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o",                                "R", "s", "t", "T", "u", "U", "y"};            // Output date and time using each standard format string.            foreach (string standardFmt in standardFmts)                Console.WriteLine("{0}: {1}", standardFmt,                                  dateValue.ToString(standardFmt));            Console.WriteLine();            // Create an array of some custom format strings.            string[] customFmts = {"yyyyMMddHHmmss","h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",                              "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };            // Output date and time using each custom format string.            foreach (string customFmt in customFmts)                Console.WriteLine("'{0}': {1}", customFmt,                                  dateValue.ToString(customFmt));        }

Output result:

  

Msdn: datetime. tostring method (string)

 

 

Iii. enumerative Format String
        enum Colors { Red, Green, Blue, Yellow = 12 };        static void EnumToString()        {            Colors myColor = Colors.Yellow;            Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));            Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));            Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));            Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));            Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);            Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));            Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));            Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));            Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));            Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));            Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));            Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));            Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));        }

Output result:

  

Msdn: enum. tostring method (string)

 

Author: samwang
Source: http://wangshenhe.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to review it. During reprinting, you must provide a link to the original article clearly. Thank you for your cooperation.

Related Article

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.