C # console formatting output summary

Source: Internet
Author: User
Format a number

Description of format characters and associated attributes

--------------------------------------------------------------------------------

C and C currency formats.

In decimal format.

E, E scientific count (INDEX) format.

F and F are fixed points.

G.

N, N numeric format.

The r and R round-trip formats Ensure that the number converted to a string has the same value as the original number when being converted back to a number.

X and X hexadecimal formats.

--------------------------------------------------------------------------------

Example:

 Double val = Math. PI; Console. WriteLine (val. ToString (); // displays 3.14159265358979
Console. WriteLine (val. ToString ("E"); // displays 3.141593E + 000
Console. WriteLine (val. ToString ("F3"); // displays 3.142
Single val2 = 0.123F; Console. WriteLine (val2.ToString ("p"); // displays 12.30%
Console. WriteLine (val2.ToString ("p1"); // displays 12.3%
Int val3 = 65535;
Console. WriteLine (val3.ToString ("x"); // displays ffff
Console. WriteLine (val3.ToString ("X"); // displays FFFF
Decimal val4 = 323.42313 m;
Console. WriteLine ("{0, 9: f2}", val4); // displays 323.42
// {0th: f2} indicates the first parameter, which occupies the right alignment of 9 digits and has two accurate decimal places.
Console. WriteLine ("{0,-9: f2}", val4); //-9 indicates 9 digits, left aligned

By default, a space is placed between the digit and the percent sign. The customization method is as follows:

The NumberFormatInfo class is a member of the System. Globalization namespace. Therefore, the namespace must be imported to the program.

 Single val=0.123F;

object myobj=NumberFormatInfo.CurrentInfo.Clone( ) as NumberFormatInfo;

NumberFormatInfo myformat=myobj as NumberFormatInfo;

myformat.PercentPositivePattern=1;

Console.WriteLine(val.ToString("p",myformat)); //displays 12.30%;

Console.WriteLine(val.ToString("p1",myformat)); //displays 12.3%;

 

Formatting is flexible. The following example demonstrates a meaningless currency structure:

 Double val = 1234567.89;

Int [] groupsize = {2, 1, 3 };

Object myobj = NumberFormatInfo. CurrentInfo. Clone ();

NumberFormatInfo mycurrency = myobj as NumberFormatInfo;

Mycurrency. CurrencySymbol = "#"; // symbol

Mycurrency. CurrencyDecimalSeparator = ":"; // decimal point

Mycurrency. CurrencyGroupSeparator = "_"; // delimiter

Mycurrency. CurrencyGroupSizes = groupsize;

Console. WriteLine (val. ToString ("C", mycurrency ));

 

// Output #1_234_5_67: 89

Format date

The output format depends on the cultural settings of the user's computer.

 using System;

using System.Globalization;

public class MainClass

{

  public static void Main(string[] args)

  {

DateTime dt = DateTime.Now;

String[] format = {"d","D","f","F","g","G","m","r","s","t", "T","u", "U","y","dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy","dddd, MMMM dd","M/yy","dd-MM-yy",};

String date;

for (int i = 0; i < format.Length; i++)

{

date = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);

Console.WriteLine(String.Concat(format, " :" , date));

}

}

}

D: 07/11/2004 <===== output

D: Sunday, 11 July 2004

F: Sunday, 11 July 2004

F: Sunday, 11 July 2004 10:52:36

G: 07/11/2004

G: 07/11/2004 10:52:36

M: July 11

R: Sun, 11 Jul 2004 10:52:36 GMT

S: 2004-07-11T10: 52: 36

T: 10: 52

T: 10: 52: 36

U: 2004-07-11 10: 52: 36Z

U: Sunday, 11 July 2004 02:52:36

Y: 2004 July

Dddd, MMMM dd yyyy: Sunday, July 11 2004

Ddd, MMM d "'" yy: Sun, Jul 11' 04

Dddd, MMMM dd: Sunday, July 11

M/yy: 7/04

Dd-MM-yy: 11-07-04

 

Format Enumeration
 enum classmen

{
ynn=1,

yly=2,

css=3,

C++=4
}

 

Obtain the enumerated string information as follows:

 classmen myclassmen=classmen.yly;

Console.WriteLine(myclassmen.ToString( )); //displays yly

Console.WriteLine(myclassmen.ToString("d")); //displays 2

The following information is obtained from the system enumeration:

 DayOfWeek day=DayOfWeek.Friday;

//displays "Day is Friday"

Console.WriteLine(String.Format("Day is {0:G}",day));

Format the string "G" to display the enumeration as a string.

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.