Description of ToString () format in C,

Source: Internet
Author: User
Tags iso 8601

Description of ToString () format in C,

When developing a serial communication program, you need to convert the 10th to the 16th to the following class capacity:

From: http://www.cnblogs.com/xdotnet/archive/2009/01/17/tostring_format.html

 

ToString formatting is used when many objects are displayed as strings. Since I have not noticed this problem before, I would like to summarize the formatting of each basic object for future use !!!

 

Int. ToString (format ):

The format string adopts the following format: Axx, where A is the format specifier, specifies the format type, xx is the precision specifier, and controls the number of valid digits or decimal places of the formatted output, as follows:

 

Format description

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 General 2.5.ToString ("G ") 2.5
N Number 2500000. ToString ("N ") 2,500,000.00
X Hexadecimal 255. ToString ("X ") FF

 

DateTime. ToString (format ):

The following format can only be used separately, indicating a specific format:

  • D ShortDatePattern
  • D LongDatePattern
  • F complete date and time (long date and short time)
  • F FullDateTimePattern (long date and long time)
  • G regular (short date and short time)
  • G regular (short date and long time)
  • M, M MonthDayPattern
  • R, R RFC1123Pattern
  • S uses SortableDateTimePattern of the local time (based on ISO 8601)
  • T ShortTimePattern
  • T LongTimePattern
  • U UniversalSortableDateTimePattern is used to display the format of common time
  • U uses the complete date and time of the general time (long date and long time)
  • Y, Y YearMonthPattern

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

  • D. A single-digit date has no leading zero.
  • A day in dd month. A single-digit date has a leading zero.
  • The abbreviation of a day in ddd Week, which is defined in AbbreviatedDayNames.
  • The full name of the Day of the dddd Week, which is defined in DayNames.
  • M month number. One-digit month does not have a leading zero.
  • MM Month number. A single-digit month has a leading zero.
  • The abbreviated name of the MMM month, which is defined in AbbreviatedMonthNames.
  • The full name of the MMMM month, which is defined in MonthNames.
  • Y does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year that does not have the leading zero is displayed.
  • Yy does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year with the leading zero is displayed.
  • Yyyy includes the four-digit year of the epoch.
  • Gg period or epoch. This mode is ignored if the date to be formatted does not have an associated period or epoch string.
  • H 12-Hour. One-digit hours do not have a leading zero.
  • The hour in hh 12-hour format. The number of hours in a single digit has a leading zero.
  • H In 24-hour format. One-digit hours do not have a leading zero.
  • The hour in the 24-hour format. The number of hours in a single digit has a leading zero.
  • M minutes. The number of minutes in a single digit does not start with zero.
  • Mm minutes. The number of minutes in a single digit 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 in a single digit has a leading zero.
  • The decimal precision of second f is one digit. The remaining numbers are truncated.
  • The decimal precision of ff seconds is two digits. The remaining numbers are truncated.
  • The decimal precision of fff seconds is three. The remaining numbers are truncated.
  • The decimal precision of ffff seconds is four digits. The remaining numbers are truncated.
  • The decimal precision of fffff seconds is five digits. The remaining numbers are truncated.
  • The decimal precision of ffffff seconds is six digits. The remaining numbers are truncated.
  • The decimal precision of fffff seconds is seven digits. The remaining numbers are truncated.
  • T the first character (if any) of the AM/PM indicator defined in AMDesignator or PMDesignator ).
  • The AM/PM indicator defined by tt in AMDesignator or PMDesignator (if any ).
  • Z Time Zone offset ("+" or "-" is followed by only the hour ). One-digit hours do not have a leading zero. For example, the Standard Time in the Pacific is-8 ".
  • Zz Time Zone offset ("+" or "-" is followed by only the hour ). The number of hours in a single digit has a leading zero. For example, the Standard Time in the Pacific is-08 ".
  • The full time zone offset of zzz ("+" or "-" followed by hours and minutes ). The number of hours and minutes in a single digit has a leading zero. For example, the Pacific Standard Time is ".
  • : Default Time delimiter defined in TimeSeparator.
  • /Default date delimiter defined in DateSeparator.
  • % C where c is the Format mode (if used separately ). If the Format mode is combined with the original and other formats, the "%" character can be omitted.
  • "C, where c is any character. Display characters according to the original meaning. To display the backslash character, use "".

Format in String. Format:

You can also Format these structure objects in the String. Format method. These formatting formats are included between "{}" and separated. ":" Is the index value of String. Format, which is the same as general formatting, and ":" is followed by the formatting type of these structures. If a placeholder is used, "," is used to separate the index. "positive" indicates the right alignment, "negative" indicates the left alignment, and "absolute" indicates the character width. For example:

String. Format ("the value is {123.45: f3 );

// The value is 123.450

7 indicates the placeholder (right-aligned), which occupies 7 digits. If-7 is used, it indicates the left-aligned position, which occupies 7 digits.

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.