Data Formatting for DataTextFormatString
Format String Description
{0:Cn} Currency. Displays numeric values in currency format with a leading dollar sign; n indicates the number of decimal places. If n is omitted, the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of digits desired in the resulting string. If necessary, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired number of decimal places. Commas are inserted between each group of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates the desired number of decimal places.
{0} Text. Displays a text string.
Custom Format Strings string.Format
If the standard numeric format specifiers do not provide the type of formatting required, you can use custom format strings, in effect, drawing a picture of the format using special characters to indicate number positions and precisions. The characters used to build a format string are shown in the following table.
Format Character Description
0 Zero placeholder. Displays a number in the position if one is present; otherwise, displays a leading or trailing zero. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
# Digit placeholder. Displays a number in the position if one is present; otherwise, displays no decimal digit. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
. Decimal point. Indicates the location of a decimal point in a formatted value.
, Thousand separator. Indicates the insertion of comma separators every three positions to the left of a decimal point.
% Percent placeholder. Converts a number to a percentage and displays percent sign in indicated position.
$ Dollar sign. Indicates position of a dollar sign to format currency values.
' ' Text characters enclosed as quotes are treated as literal strings and are inserted into the format at the indicated position.
Examples of using custom format strings to format numeric values are shown in the following table. For example, to use a format string for the ItemPrice BoundField in the above GridView, the specification can be DataFormatString="{0:$#,#.##}" to produce the same display as the standard currency string DataFormatString="{0:C}".
Value Format String Result
1234.567 {0:0.00} 1234.57
1234.567 {0:00000.0000} 01234.5670
1234.567 {0:#####.##} 1234.57
1234.567 {0:#.###} 1234.567
1234.567 {0:#.#} 1234.6
1234.567 {0:#,#.##} 1,234.57
1234.567 {0:$#,#.##} $1,234.57
1234.567 {0:$ #,#.##} $ 1,234.57
1234.567 {0:($ #,#.##)} ($ 1,234.57)
-1234.567 {0:#,#.##} -1,234.57
.1234 {0:#%} 12%
.1234 {0:Percent = #.0%} Percent = 12.3%
没有评论:
发表评论