C # format time, currency

Source: Internet
Author: User

1. Formatted currency (related to system environment, Chinese system default format RMB, English system format USD)

String. Format ("{0:c}", 0.2) results are: ¥0.20 (English operating system results: $0.20)

The default formatting retains two decimal places after the decimal point, and if you need to keep one or more, you can specify the number of digits
String. Format ("{0:c1}", 23.15) Result: ¥23.2 (intercept will be automatically rounded)

Formatting multiple instances of object
String. Format ("market price: {0:C}, preferential price {1:C}", 23.15,19.82)

2, formatted decimal number (formatted as a fixed number of digits, the number of digits can not be less than unformatted before, only support shaping)

String. Format ("{0:d3}", 23) result is: 023

String. The result of Format ("{0:d2}", 1223) is: 1223, (the precision specifier indicates the minimum number of digits required in the resulting string.) )

3. A semicolon-delimited number, and specify the number of digits after the decimal point

String. Format ("{0:n}", 14200) Results: 14,200.00 (default is two digits after decimal point)

String. Format ("{0:n3}", 14200.2458) Result: 14,200.246 (auto rounding)

4. Percentage of formatting

String. Format ("{0:p}", 0.24583) result is: 24.58% (default of two decimal places reserved)

String. Format ("{0:p1}", 0.24583) result is: 24.6% (auto rounding)

5, zero placeholder and digit placeholder

String. The result of Format ("{0:0000.00}", 12394.039) is: 12394.04

String. The result of Format ("{0:0000.00}", 194.039) is: 0194.04

String. Format ("{0:###.##}", 12394.039) result is: 12394.04

String. Format ("{0:####.#}", 194.039) result is: 194

The following explanation is difficult to understand, more test the actual application can be understood.
Zero placeholder:
If the formatted value has a number in the format string where "0" appears, this number is copied to the result string. The position of the leftmost "0" before the decimal point and the rightmost "0" after the decimal point determine the total number range that appears in the result string.
The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, where 0 bits are always removed.

Digit placeholder:
If the formatted value has a number in the format string where "#" appears, the number is copied to the result string. Otherwise, no value is stored at this location in the resulting string.
Note that if "0" is not a valid number, this specifier never displays the "0" character, even if "0" is the only number in the string. If "0" is a valid number in the displayed number, the "0" character is displayed.
The "# #" format string allows the value to be rounded to the nearest digit before the decimal point, where 0 is always removed.

6. Date formatting

String. Format ("{0:d}", System.DateTime.Now) result is: 2009-3-20 (month position is not 03)

String. Format ("{0:d}", System.DateTime.Now) results are: March 20, 2009

String. Format ("{0:f}", System.DateTime.Now) results are: March 20, 2009 15:37

String. Format ("{0:f}", System.DateTime.Now) results are: March 20, 2009 15:37:52

String. Format ("{0:g}", System.DateTime.Now) results are: 2009-3-20 15:38

String. Format ("{0:g}", System.DateTime.Now) results are: 2009-3-20 15:39:27

String. Format ("{0:m}", System.DateTime.Now) results are: March 20

String. The result of Format ("{0:t}", System.DateTime.Now) is: 15:41

String. The result of Format ("{0:t}", System.DateTime.Now) is: 15:41:50


For more detailed instructions please follow Microsoft's instructions on this or inquire on MSDN.

Microsoft MSDN's method description for String.Format:

Name Description
String.Format (String, Object) replaces the format item in the specified String with the text equivalent of the value of the specified Object instance.
String.Format (String, object[]) replaces the format item in the specified string with the text equivalent of the value of the corresponding Object instance in the specified array.
String.Format (IFormatProvider, String, object[]) replaces the format item in the specified string with the text equivalent of the value of the corresponding Object instance in the specified array. The specified parameter provides culture-specific formatting information.
String.Format (String, Object, object) replaces the format item in the specified string with the text equivalent of a value of two specified Object instance.
String.Format (String, Object, object, object) replaces the format item in the specified string with the text equivalent of a value of three specified Object instance.

Standard numeric format string

Format specifier name description
C or C
Currency
The number is converted to a string that represents the monetary amount. The conversion is controlled by the currency format information of the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default currency precision given by the current NumberFormatInfo object is used.

D or D
Decimal number
This format is supported only for integer types. A string that is converted to a decimal number (0-9), preceded by a minus sign, if the number is negative.

The precision specifier indicates the minimum number of digits required in the resulting string. If necessary, fill the left side of the number with 0 to produce the number of digits given by the precision specifier.

E or E
Science Notation (index)
The number is converted to "-d.ddd ... A string in the form of e+ddd "or"-d.ddd...e+ddd ", where each" D "represents a number (0-9). If the number is negative, the string begins with a minus sign. There is always a number before the decimal point.

The precision specifier indicates the number of digits required after the decimal point. If the precision specifier is omitted, the default value, six digits after the decimal point, is used.

The case of the format specifier indicates whether to prefix "E" or "E" before the exponent. The index is always composed of a plus or minus sign and a minimum of three digits. If required, fill the index with 0 to meet the requirement of a minimum of three digits.

F or F
Sentinel
The number is converted to a string in the form "-ddd.ddd ...", where each "D" represents a number (0-9). If the number is negative, the string begins with a minus sign.

The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

G or G
Conventional
Depending on the number type and whether there is a precision specifier, the number is converted to the most compact form of a fixed-point or scientific notation. If the precision specifier is omitted or zero, the type of the number determines the default precision, as shown in the following table.

Byte or Sbyte:3

Int16 or Uint16:5

Int32 or Uint32:10

Int64 or uint64:19

Single:7

Double:15

Decimal:29

If the exponent is greater than 5 and less than the precision specifier when the number is expressed in scientific notation, the fixed-point notation is used; otherwise, scientific notation is used. If a decimal point is required, and trailing zeros are ignored, the result contains a decimal points. If the precision specifier exists and the number of significant digits in the result exceeds the specified precision, the excess trailing digits are removed by rounding.

There is one exception to the above rule: if the number is Decimal and the precision specifier is omitted. In this case, the fixed point notation is always used and trailing zeros are retained.

When using scientific notation, if the format specifier is "G", the exponent of the result is prefixed with "E", and if the format specifier is "G", the exponent of the result is prefixed with "E".

N or N
Digital
The number is converted to a string in the form "-d,ddd,ddd.ddd ...", where "-" represents a negative sign (if needed), "D" represents a number (0-9), "," represents a thousands separator between numeric groups, "." Represents the decimal point symbol. The actual negative pattern, number group size, thousands separator, and decimal separator are specified by the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

P or P
Percentage
The number is converted to a string that represents a percentage, defined by the Numberformatinfo.percentnegativepattern or Numberformatinfo.percentpositivepattern property, The former is used in the case where the number is negative and the latter is used for positive numbers. The converted number is multiplied by 100 to represent a percentage.

The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

R or R
Roundtrip process
This format is supported only for single and Double types. The round-trip specifier guarantees that the numeric value converted to a string is parsed again into the same value. When you use this specifier to format a numeric value, you first test it with the general format: Double uses 15-bit precision, and single uses 7-bit precision. If this value is parsed back to the same value successfully, it is formatted with the general format specifier. However, if this value is not successfully parsed to the same numeric value, it is formatted like this: Double uses 17-bit precision, and single uses 9-bit precision.

Although there can be a precision specifier here, it is ignored. When this specifier is used, the round-trip process takes precedence over precision.

X or X
Hexadecimal number
This format is supported only for integer types. A string that converts a number to a hexadecimal number. The case of the format specifier indicates whether to use uppercase characters or lowercase characters for hexadecimal digits greater than 9. For example, use "X" to produce "ABCDEF" and "X" to produce "ABCDEF".

The precision specifier indicates the desired maximum in the result string.

Math.Round (3.45, 2, Midpointrounding.awayfromzero);

C # format time, currency

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.