The ToString () format in C #

Source: Internet
Author: User
Tags iso 8601 truncated

Int.tostring (format):

The format string takes the form: Axx, where a is the format specifier, specifies the format type, XX is the precision specifier, and controls the number of significant digits or decimal digits of the formatted output, as follows:

Format specifier

Description Example Output
C Currency 2.5.ToString ("C") ¥2.50
D Decimal number 25.ToString ("D5") 00025
E Scientific type 25000.ToString ("E") 2.500000E+005
F Fixed point 25.ToString ("F2") 25.00
G Conventional 2.5.ToString ("G") 2.5
N Digital 2500000.ToString ("N") 2,500,000.00
X Hexadecimal 255.ToString ("X") Ff


DateTime.ToString (format):

The following format can only be used alone to represent a specific format:

    • D ShortDatePattern
    • D Longdatepattern
    • F Full Date and time (long date and short time)
    • F Fulldatetimepattern (long date and long time)
    • G General (short date and short time)
    • G General (short date and long time)
    • M, M Monthdaypattern
    • R, R Rfc1123pattern
    • s use local time Sortabledatetimepattern (based on ISO 8601)
    • T Shorttimepattern
    • T Longtimepattern
    • U universalsortabledatetimepattern format for displaying the Universal Time
    • U use the full date and time of the universal Time (long date and long time)
    • Y, y Yearmonthpattern

The following formats can be used in combination to format different date display formats:

  • A day in the D month. One-digit date has no leading zeros.
  • DD a day of the month. A one-digit date has a leading zero.
  • The abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
  • dddd the full name of the day of the week, as defined in DayNames.
  • M-month number. One-digit month has no leading zeros.
  • MM month number. One-digit month has a leading zero.
  • Abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
  • The full name of the MMMM month, as defined in MonthNames.
  • Y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
  • YY does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
  • The YYYY includes the four-digit year of the era.
  • GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
  • H 12 Hour hour system. One-digit hours do not have leading zeros.
  • HH 12-hour hour. One-digit hours have leading zeros.
  • H 24 hour hour system. One-digit hours do not have leading zeros.
  • HH 24-hour hour. One-digit hours have leading zeros.
  • M minutes. A single-digit number of minutes does not have a leading zero.
  • MM minutes. A single-digit number of minutes has a leading zero.
  • s seconds. The number of seconds in a single digit does not have a leading zero.
  • SS seconds. The number of seconds of one digit has a leading zero.
  • The fractional precision of the F-second is one digit. The remaining digits are truncated.
  • The fractional precision of the FF seconds is two bits. The remaining digits are truncated.
  • The fractional precision of FFF seconds is three bits. The remaining digits are truncated.
  • The fractional precision of ffff seconds is four bits. The remaining digits are truncated.
  • The fractional precision of fffff seconds is five bits. The remaining digits are truncated.
  • The fractional precision of ffffff seconds is six bits. The remaining digits are truncated.
  • The fractional precision of fffffff seconds is seven bits. The remaining digits are truncated.
  • t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
  • The AM/PM indicated by the TT in AMDesignator or PMDesignator (if present).
  • The Z Time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8".
  • ZZ Time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08".
  • The ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00".
  • : The default time delimiter defined in TimeSeparator.
  • /The default date delimiter defined in DateSeparator.
  • % c where C is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns.
  • "C where c is any character. Displays the characters as they are originally defined. To display a backslash character, use the.

Use formatting in String.Format:

These structure objects can also be formatted in the String.Format method, which are formatted as "{}" and separated by ":". ":" The index value preceded by String.Format is the same as for general formatting, and ":" is followed by the format type of these structures. If you use placeholders, use "," split between and indexes, positive numbers for right alignment, negative numbers for left alignment, and absolute values for the number of character widths, for example:

String. Format ("Thevalue is {0,7:f3}",123.45);

Results The value is 123.450

Where: 7 means placeholder (right-aligned), 7-bit, or 7 for left-aligned, seven-bit.

Original address: http://www.cnblogs.com/huangting2009/archive/2009/02/09/1386596.html

The ToString () format in C #

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.