分享

ASP.NET2.0 绑定数据字段字符串格式

 趋明 2012-05-25
ASP.NET2.0 绑定数据字段字符串格式
2008-01-25 17:47

格式字符串 结果
{0:d}     YY-MM-DD
{0:p}     百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2}    $12.68
{0:d}      3/23/2003       
{0:T}     12:00:00 AM
{0:男;;女}

格式字符串 资料 结果
"{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

格式字符串
1、常用的数值格式字符串:
C或c 货币格式
D或d 十进制格式
E或e 科学(指数)格式
F或f 固定小数位格式
G或g 一般格式
N或n 数字格式
P或p 百分比格式
X或x 十六进制格式

2、常用的日期格式字符串:
d      短日期
D      长日期
f      完整(长日期和短时间)
F      完整(长日期和长时间)
g      一般(短日期和短时间)
G      一般(短日期和长时间)
M或m   月和日
R或r   RFC1123格式
s      使用本地时间的ISO8601可分类
t      短时间
T      长时间
u      使用通用时间的ISO8601可分类
U      通用可分类日期/时间
Y或y   年和月

3、数字的图片格式字符
0 没有有效值则显示0
# 数字占位符
. 小数点
, 分隔符
% 百分比
E+0,E-0,e+0或e-0 将输出格式为科学或指数含义
\ 转义字符
"或' 它们之间的任何字符都被解释为字符串
{和} 用于显示单文字性花括号,如"{{"显示为"{"
;分隔格式字符串中正、负和零值的两个或三个部分

标准 DateTime 格式字符串包含下表中的一个格式说明符字符。如果下表中没有该格式说明符,将引发运行时异常。如果格式字符串在长度上比单个字符长(即使多出的字符是空白),则格式字符串被解释为自定义格式字符串。

请注意,这些格式说明符产生的输出字符串受“区域选项”控制面板中的设置的影响。计算机的区域性设置或日期和时间设置不同,将生成不同的输出字符串。

格 式字符串显示的时间和日期分隔符由与当前区域性的 DateTimeFormat 属性关联的 DateSeparator 和 TimeSeparator 字符定义。然而,如果 InvariantCulture 被“r”、“s”和“u”说明符引用,与 DateSeparator 和 TimeSeparator 字符关联的字符不随当前区域性更改。

下表描述了用来格式化 DateTime 对象的标准格式说明符。

格式说明符 名称 说明
d 短日期模式 显示由与当前线程关联的 DateTimeFormatInfo.ShortDatePattern 属性定义的模式或者由指定格式提供程序定义的模式。
D 长日期模式 显示由与当前线程关联的 DateTimeFormatInfo.LongDatePattern 属性定义的模式或者由指定格式提供程序定义的模式。
t 短时间模式 显示由与当前线程关联的 DateTimeFormatInfo.ShortTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。
T 长时间模式 显示由与当前线程关联的 DateTimeFormatInfo.LongTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。
f 完整日期/时间模式(短时间) 显示长日期和短时间模式的组合,由空格分隔。
F 完整日期/时间模式(长时间) 显示由与当前线程关联的 DateTimeFormatInfo.FullDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。
g 常规日期/时间模式(短时间) 显示短日期和短时间模式的组合,由空格分隔。
G 常规日期/时间模式(长时间) 显示短日期和长时间模式的组合,由空格分隔。
M 或 m 月日模式 显示由与当前线程关联的 DateTimeFormatInfo.MonthDayPattern 属性定义的模式或者由指定格式提供程序定义的模式。
R 或 r RFC1123 模式 显示由与当前线程关联的 DateTimeFormatInfo.RFC1123Pattern 属性定义的模式或者由指定格式提供程序定义的模式。这是定义的标准,并且属性是只读的;因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相 同的。属性引用 CultureInfo.InvariantCulture 属性并遵照自定义模式“ddd, dd MMM yyyy HH:mm:ss G\MT”。请注意,“GMT”中的“M”需要转义符,因此它不被解释。格式化并不修改 DateTime 的值,所以您必须在格式化之前将值调整为 GMT。
s 可排序的日期/时间模式;符合 ISO 8601 显示由与当前线程关联的 DateTimeFormatInfo.SortableDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。属性引用 CultureInfo.InvariantCulture 属性,格式遵照自定义模式“yyyy-MM-ddTHH:mm:ss”。
u 通用的可排序日期/时间模式 显示由与当前线程关联的 DateTimeFormatInfo.UniversalSortableDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。因为它是定义的标准,并且属性是只读的,因此无论区域性或格式提供程序是什么,模式总是相同的。格式化 遵照自定义模式“yyyy-MM-dd HH:mm:ssZ”。格式化日期和时间时不进行时区转换;所以,请在使用格式说明符之前将本地日期和时间转换为通用时间。
U 通用的可排序日期/时间模式 显示由与当前线程关联的 DateTimeFormatInfo.FullDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。请注意,显示的时间是通用时间,而不是本地时间。
Y 或 y 年月模式 显示由与当前线程关联的 DateTimeFormatInfo.YearMonthPattern 属性定义的模式或者由指定格式提供程序定义的模式。
任何其他单个字符 未知说明符

下面的示例说明了标准格式字符串如何与 DateTime 对象一起使用。

[Visual Basic]
Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")

' Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"

' Use the DateTimeFormat from the culture associated
' with the current thread.

Console.WriteLine( dt.ToString("d") )
Console.WriteLine( dt.ToString("m") )

' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )

' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )

[C#]
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");

// Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";

// Use the DateTimeFormat from the culture associated
// with the current thread.
Console.WriteLine( dt.ToString("d") );
Console.WriteLine( dt.ToString("m") );

// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );

// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );

// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );

format函数
Dim   MyDateTime   As   Date   =   #1/27/2001   5:04:23   PM#
Dim   MyStr   As   String
'   Returns   current   system   time   in   the   system-defined   long   time   format.
MyStr   =   Format(Now(),   "Long   Time")
'   Returns   current   system   date   in   the   system-defined   long   date   format.
MyStr   =   Format(Now(),   "Long   Date")
'   Also   returns   current   system   date   in   the   system-defined   long   date
'   format,   using   the   single   letter   code   for   the   format.
MyStr   =   Format(Now(),   "D")
'   Returns   the   value   of   MyDateTime   in   user-defined   date/time   formats.
MyStr   =   Format(MyDateTime,   "h:m:s")       '   Returns   "5:4:23".
MyStr   =   Format(MyDateTime,   "hh:mm:ss   tt")       '   Returns   "05:04:23   PM".
MyStr   =   Format(MyDateTime,   "dddd,   MMM   d   yyyy")       '   Returns   "Saturday,
'   Jan   27   2001".
MyStr   =   Format(MyDateTime,   "HH:mm:ss")       '   Returns   "17:04:23"
MyStr   =   Format(23)       '   Returns   "23".
'   User-defined   numeric   formats.
MyStr   =   Format(5459.4,   "##,##0.00")       '   Returns   "5,459.40".
MyStr   =   Format(334.9,   "###0.00")       '   Returns   "334.90".
MyStr   =   Format(5,   "0.00%")       '   Returns   "500.00%".

string类型的转换
CBool   示例
本示例使用   CBool   函数将表达式转换为   Boolean   值。如果表达式的计算结果为非零值,CBool   将返回   True;否则返回   False。
Dim   A,   B,   C   As   Integer
Dim   Check   As   Boolean
A   =   5
B   =   5
Check   =   CBool(A   =   B)       '   Check   is   set   to   True.
'   ...
C   =   0
Check   =   CBool(C)       '   Check   is   set   to   False.

CByte   示例
本示例使用   CByte   函数将表达式转换为   Byte。
Dim   MyDouble   As   Double
Dim   MyByte   As   Byte
MyDouble   =   125.5678
MyByte   =   CByte(MyDouble)       '   MyByte   is   set   to   126.

CChar   示例
本示例使用   CChar   函数将   String   表达式的第一个字符转换为   Char   类型。
Dim   MyString   As   String
Dim   MyChar   As   Char
MyString   =   "BCD"       '   CChar   converts   only   first   character   of   string.
MyChar   =   CChar(MyString)       '   MyChar   is   set   to   "B".
CChar   的输入参数必须是   String   数据类型。由于   CChar   不能接受数值数据类型,因此无法使用   CChar   将数字转换为字符。本示例获取一个表示代码数据点(字符代码)的数字,然后将其转换为对应的字符。它使用   InputBox   获取数字字符串,并使用   CInt   将该字符串转换为   Integer   类型,然后使用   ChrW   将该数字转换为   Char   类型。
Dim   MyDigits   As   String       '   Input   string   of   digits   to   be   converted.
Dim   CodePoint   As   Integer       '   Number   to   be   represented   as   a   character.
Dim   MyChar   As   Char
MyDigits   =   InputBox("Enter   code   point   of   character:")
CodePoint   =   CInt(MyDigits)       '   Convert   entire   string   to   Integer.
MyChar   =   ChrW(CodePoint)       '   MyChar   is   set   to   Char   value   of   code   point.

CDate   示例
本示例使用   CDate   函数将字符串转换为   Date   值。通常,建议不要使用硬编码的日期和时间作为字符串(如下例所示)。而应使用日期文本和时间文本,如   #Feb   12、1969#   和   #4:45:23   PM#。
Dim   MyDateString,   MyTimeString   As   String
Dim   MyDate,   MyTime   As   Date
MyDateString   =   "February   12,   1969"
MyTimeString   =   "4:35:47   PM"
'   ...
MyDate   =   CDate(MyDateString)       '   Convert   to   Date   data   type.
MyTime   =   CDate(MyTimeString)       '   Convert   to   Date   data   type.

CDbl   示例
本示例使用   CDbl   函数将表达式转换为   Double。
Dim   MyDec   As   Decimal
Dim   MyDouble   As   Double
MyDec   =   234.456784D         '   Literal   type   character   D   makes   MyDec   a   Decimal.
MyDouble   =   CDbl(MyDec   *   8.2D   *   0.01D)       '   Convert   result   to   a   Double.

CDec   示例
本示例使用   CDec   函数将数值转换为   Decimal。
Dim   MyDouble   As   Double
Dim   MyDecimal   As   Decimal
MyDouble   =   10000000.0587
MyDecimal   =   CDec(MyDouble)       '   Convert   to   Decimal.

CInt   示例
本示例使用   CInt   函数将一个值转换为   Integer。
Dim   MyDouble   As   Double
Dim   MyInt   As   Integer
MyDouble   =   2345.5678
MyInt   =   CInt(MyDouble)       '   MyInt   is   set   to   2346.

CLng   示例
本示例使用   CLng   函数将多个值转换为   Long。
Dim   MyDbl1,   MyDbl2   As   Double
Dim   MyLong1,   MyLong2   As   Long
MyDbl1   =   25427.45
MyDbl2   =   25427.55
MyLong1   =   CLng(MyDbl1)       '   MyLong1   contains   25427.
MyLong2   =   CLng(MyDbl2)       '   MyLong2   contains   25428.

CObj   示例
本示例使用   CObj   函数将一个数值转换为   Object。Object   变量本身只包含一个四字节的指针,该指针指向赋给   Object   变量的   Double   值。
Dim   MyDouble   As   Double
Dim   MyObject   As   Object
MyDouble   =   2.7182818284
MyObject   =   CObj(MyDouble)       '   Double   value   is   pointed   to   by   MyObject.

CShort   示例
本示例使用   CShort   函数将一个数值转换为   Short。
Dim   MyByte   as   Byte
Dim   MyShort   as   Short
MyByte   =   100
MyShort   =   CShort(MyByte)       '   Convert   to   Short.

CSng   示例
本示例使用   CSng   函数将多个值转换为   Single。
Dim   MyDouble1,   MyDouble2   As   Double
Dim   MySingle1,   MySingle2   As   Single
MyDouble1   =   75.3421105
MyDouble2   =   75.3421567
MySingle1   =   CSng(MyDouble1)       '   MySingle1   is   set   to   75.34211.
MySingle2   =   CSng(MyDouble2)       '   MySingle2   is   set   to   75.34216.

CStr   示例
本示例使用   CStr   函数将一个数值转换为字符串。
Dim   MyDouble   As   Double
Dim   MyString   As   String
MyDouble   =   437.324
MyString   =   CStr(MyDouble)       '   MyString   is   set   to   "437.324".
本示例使用   CStr   函数将多个   Date   值转换为   String   值。
Dim   MyDate   As   Date
Dim   MyString   As   String
'   ...
MyDate   =   #February   12,   1969   00:00:00#       '   INVALID   format.
'   Date   literals   must   be   in   the   format   #m/d/yyyy#   or   they   are   invalid.
'   ...
MyDate   =   #2/12/69   00:00:00#       '   Time   is   midnight.
'   The   neutral   time   value   of   00:00:00   is   suppressed   in   the   conversion.
MyString   =   CStr(MyDate)       '   MyString   is   set   to   "2/12/1969".
'   ...
MyDate   =   #2/12/69   00:00:01#       '   Time   is   one   second   past   midnight.
'   The   time   component   becomes   part   of   the   converted   value.
MyString   =   CStr(MyDate)       '   MyString   is   set   to   "2/12/1969   12:00:01   AM".
CStr   始终以符合当前区域设置的标准短格式呈现   Date   值,如“6/15/2003   4:35:47   PM”。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多