dataformatstring--format string

Source: Internet
Author: User

The following points need to be noted here
1: Because 2.0 for security reasons, also set HTMLEncode = False to enable dataformatstring to take effect. Asp:boundfield in the GridView use DataFormatString must set the property htmlencode= "False", otherwise it will not work.

2: If you need to use a format string of date types, the corresponding field in the data entity should also be of the date type.

3: Format string C represents a currency unit, and the data type that needs to be bound should be of a numeric type. If the string type does not work, you need to manually add the formatted string to dataformatstring= "¥{0:c}".

Use the DataFormatString property to provide a custom format for the items in the column.

Gets or sets the string that displays the display format for the items in the specified column. A formatted string that specifies the display format for the items in the column, and the default value is String.Empty.

The data format string consists of two parts, separated by a colon, in the form {a:bxx}. For example, the format string {0:F2} will display a fixed-point number with a two-bit decimal.

The entire string must be enclosed in curly braces to indicate that it is a format string rather than an actual string. Any text outside the curly braces is displayed as the actual text.

The value before the colon specifies the index of the parameter in the zero-based argument list. The value can only be set to 0 because there is only one value per cell.

The character after the colon specifies the display format for the value. The following table lists some common formats.

Program:

public partial class FormatStrings:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Double x = 2005.5;
int y = 128;
Double z = 0.25;

  response.write (x.tostring () + "C2 formatted" + String.Format ("{0:C2}", X) + "<BR>");
  response.write (y.tostring () + "after D format" + String.Format ("{0:d}", y) + "<BR>");
  response.write (x.tostring () + "E2 formatted" + String.Format ("{0:e2}", X) + "<BR>");
  response.write (x.tostring () + "F4 formatted" + String.Format ("{0:f4}", X) + "<BR>");
  response.write (x.tostring () + "after G format" + String.Format ("{0:g}", X) + "<BR>");
  response.write (x.tostring () + "N3 formatted" + String.Format ("{0:n3}", X) + "<BR>");
  response.write (z.tostring () + "after P format" + String.Format ("{0:p}", Z) + "<BR>");
  response.write (y.tostring () + "after X format" + String.Format ("{0:x}", y) + "<BR>");
  response.write (x.tostring () + "00####.00 formatted" + String.Format ("{0:00####.00}", X) + "<BR>"); br>    }

Effect:

2005.5 after the C2 format ¥2,005.50
128 after the D-format 128
2005.5 after the E2 format of 2.01E+003
2005.5 after the F4 format of 2005.5000
2005.5 after the G-format 2005.5
2005.5 after the N3 format 2,005.500
0.25 after the P-format 25%
128 after the X format 80
2005.5 after the 00####.00 format of 002005.50

More:

Dataformatstring= ' {0: format string} '

{0} in dataformatstring represents the data itself, while the format string following the colon represents the format in which the data is expected to be displayed;

Number, Currency format:
After the specified format symbol, you can specify the number of digits to display for the decimal. For example, the original data is "1.56", if the format is set to {0:n1}, then the output is "1.5". Its commonly used numerical formats are shown in the following table:

format string input result
' {0:c} ' 12345.6789 $12,345.68
' {0:c} '-12345.6789 ($12,345.68)
"{0:d}" 12345 12345
"{0:d8}" 12345 00012345
' {0:e} ' 12345.6789 1234568E+004
' {0:e10} ' 12345.6789 1.2345678900E+004
"{0:f}" 12345.6789 12345.68
"{0:f0}" 12345.6789 12346
"{0:g}" 12345.6789 12345.6789
' {0:g7} ' 123456789 1.234568E8
' {0:n} ' 12345.6789 12,345.68
' {0:n4} ' 123456789 123,456,789.0000
Total: {0:c} ' 12345.6789 total: $12345.68

Popular date-time formats:

Format Description Output format
D Compact Date format mm/dd/yyyy
D Detail Date format dddd, MMMM DD, yyyy
F Full Format (long date + short time) dddd, MMMM dd, yyyy hh:mm
F full DateTime format (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss
G General format (short date + short time) mm/dd/yyyy hh:mm
G General Format (short date + long time) mm/dd/yyyy HH:mm:ss
M,m month-day format MMMM DD
s moderate date time format Yyyy-mm-dd HH:MM:SS
T streamlined time format hh:mm
T Verbose time Format HH:mm:ss

<%# String.Format ("{0:yyyy-mm-dd}", Eval ("Effectivedate"))%>

<%# String.Format ("{0:yyyy-m-d}", DataBinder.Eval (Container.DataItem, "effectivedate"))%>

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.