转自:http://ba527.blog.163.com/blog/static/3481305200842753230257/
设置数字格式
下表列出了常用的 .NET 数字格式设置字符串。
格式字符串 名称
C 或 c 货币
D 或 d 小数
E 或 e 科学记数法
F 或 f 固定点
G 或 g 常规
N 或 n 数量
P 或 p 百分比
R 或 r 往返
X 或 x 十六进制
您可以将许多格式字符串修改为包含精度说明符,该说明符用于定义小数点后的位数。例如,格式设置字符串 D0 将数字格式设置为小数点后没有数字。您还可以使用自定义的格式设置字符串,例如 #,###。
设置日期格式
下表列出了常用的 .NET Framework 日期格式设置字符串。
格式字符串 名称
d 短日期[2008.08.08]
D 长日期[2008年08月08日]
t 短时间
T 长时间
f 完整日期/时间(短时间)
F 完整日期/时间(长时间)
g 常规日期/时间(短时间)[2008.08.08 8:8]
G 常规日期/时间(长时间)
M 或 m 月日
R 或 r RFC1123 模式
Y 或 y 年月
您还可以使用自定义的格式设置字符串,例如 dd/MM/yy。有关 .NET Framework 格式设置字符串的详细信息,请参阅Formatting Types。
例:
说明:如果参考进价>=100000,则保留1位小数,否则保留2位小数.
=IIF(Fields!参考进价.Value>=100000,"F1" , "F2")
PublicSub Main()
' Display string representations of numbers for en-us culture
Dim ciAsNew CultureInfo("en-us")
' Output floating point values
Dim floatingAs Double = 10761.937554
Console.WriteLine("C: {0}", _
floating.ToString("C", ci)) ' Displays "C: $10,761.94"
Console.WriteLine("E: {0}", _
floating.ToString("E03", ci)) ' Displays "E: 1.076E+004"
Console.WriteLine("F: {0}", _
floating.ToString("F04", ci)) ' Displays "F: 10761.9376"
Console.WriteLine("G: {0}", _
floating.ToString("G", ci)) ' Displays "G: 10761.937554"
Console.WriteLine("N: {0}", _
floating.ToString("N03", ci)) ' Displays "N: 10,761.938"
Console.WriteLine("P: {0}", _
(floating/10000).ToString("P02", ci)) ' Displays "P: 107.62 %"
Console.WriteLine("R: {0}", _
floating.ToString("R", ci)) ' Displays "R: 10761.937554"
Console.WriteLine()
' Output integral values
Dim integralAs Integer = 8395
Console.WriteLine("C: {0}", _
integral.ToString("C", ci)) ' Displays "C: $8,395.00"
Console.WriteLine("D: {0}", _
integral.ToString("D6")) ' Displays D: 008395""
Console.WriteLine("E: {0}", _
integral.ToString("E03", ci)) ' Displays "E: 8.395E+003"
Console.WriteLine("F: {0}", _
integral.ToString("F01", ci)) ' Displays "F: 8395.0"
Console.WriteLine("G: {0}", _
integral.ToString("G", ci)) ' Displays "G: 8395"
Console.WriteLine("N: {0}", _
integral.ToString("N01", ci)) ' Displays "N: 8,395.0"
Console.WriteLine("P: {0}", _
(integral/10000).ToString("P02", ci)) ' Displays "P: 83.95 %"
Console.WriteLine("X: 0x{0}", _
integral.ToString("X", ci)) ' Displays "X: 0x20CB"
Console.WriteLine()
EndSub
PublicSharedSub Main()
Dim msgShortDateAs String ="(d) Short date: . . . . . . . "
Dim msgLongDateAs String = "(D) Long date:. . . . . . . . "
Dim msgShortTimeAs String ="(t) Short time: . . . . . . . "
Dim msgLongTimeAs String = "(T) Long time:. . . . . . . . "
Dim msgFullDateShortTimeAs String = _
"(f) Full date/short time: . . "
Dim msgFullDateLongTimeAs String = _
"(F) Full date/long time:. . . "
Dim msgGeneralDateShortTimeAs String = _
"(g) General date/short time:. "
Dim msgGeneralDateLongTimeAs String = _
"(G) General date/long time (default):" & vbCrLf & _
" . . . . . . . . . . . . . "
Dim msgMonthAs String = "(M) Month:. . . . . . . . . . "
Dim msgRFC1123As String = "(R) RFC1123:. . . . . . . . . "
Dim msgSortableAs String = "(s) Sortable: . . . . . . . . "
Dim msgUniSortInvariantAs String = _
"(u) Universal sortable (invariant):" & vbCrLf & _
" . . . . . . . . . . . . . "
Dim msgUniFullAs String = "(U) Universal full date/time: "
Dim msgYearAs String = "(Y) Year: . . . . . . . . . . "
Dim msgRoundtripLocalAs String = "(o) Roundtrip (local):. . . . "
Dim msgRoundtripUTCAs String = "(o) Roundtrip (UTC):. . . . . "
Dim msgRoundtripUnspecifiedAs String = "(o) Roundtrip (Unspecified):. "
Dim msg1As String ="Use ToString(String) and the current thread culture." & vbCrLf
Dim msg2As String ="Use ToString(String, IFormatProvider) and a specified culture." & vbCrLf
Dim msgCultureAs String = "Culture:"
Dim msgThisDateAs String = "This date and time: {0}" & vbCrLf
Dim thisDateAs DateTime = DateTime.Now
Dim utcDate As DateTime = thisDate.ToUniversalTime()
Dim unspecifiedDateAs DateTime = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified)
Dim ciAs CultureInfo
' Format the current date and time in various ways.
Console.Clear()
Console.WriteLine("Standard DateTime Format Specifiers:" & vbCrLf)
Console.WriteLine(msgThisDate, thisDate)
Console.WriteLine(msg1)
' Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture
Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)
Console.WriteLine(msgShortDate & thisDate.ToString("d"))
Console.WriteLine(msgLongDate & thisDate.ToString("D"))
Console.WriteLine(msgShortTime & thisDate.ToString("t"))
Console.WriteLine(msgLongTime & thisDate.ToString("T"))
Console.WriteLine(msgFullDateShortTime & thisDate.ToString("f"))
Console.WriteLine(msgFullDateLongTime & thisDate.ToString("F"))
Console.WriteLine(msgGeneralDateShortTime & thisDate.ToString("g"))
Console.WriteLine(msgGeneralDateLongTime & thisDate.ToString("G"))
Console.WriteLine(msgMonth & thisDate.ToString("M"))
Console.WriteLine(msgRFC1123 & utcDate.ToString("R"))
Console.WriteLine(msgSortable & thisDate.ToString("s"))
Console.WriteLine(msgUniSortInvariant & utcDate.ToString("u"))
Console.WriteLine(msgUniFull & thisDate.ToString("U"))
Console.WriteLine(msgYear & thisDate.ToString("Y"))
Console.WriteLine(msgRoundtripLocal & thisDate.ToString("o"))
Console.WriteLine(msgRoundtripUTC & utcDate.ToString("o"))
Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"))
Console.WriteLine()
' Display the same values using a CultureInfo object. The CultureInfo class
' implements IFormatProvider.
Console.WriteLine(msg2)
' Display the culture used to format the values.
ci = New CultureInfo("de-DE")
Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)
Console.WriteLine(msgShortDate & thisDate.ToString("d", ci))
Console.WriteLine(msgLongDate & thisDate.ToString("D", ci))
Console.WriteLine(msgShortTime & thisDate.ToString("t", ci))
Console.WriteLine(msgLongTime & thisDate.ToString("T", ci))
Console.WriteLine(msgFullDateShortTime & thisDate.ToString("f", ci))
Console.WriteLine(msgFullDateLongTime & thisDate.ToString("F", ci))
Console.WriteLine(msgGeneralDateShortTime & thisDate.ToString("g", ci))
Console.WriteLine(msgGeneralDateLongTime & thisDate.ToString("G", ci))
Console.WriteLine(msgMonth & thisDate.ToString("M", ci))
Console.WriteLine(msgRFC1123 & utcDate.ToString("R", ci))
Console.WriteLine(msgSortable & thisDate.ToString("s", ci))
Console.WriteLine(msgUniSortInvariant & utcDate.ToString("u", ci))
Console.WriteLine(msgUniFull & thisDate.ToString("U", ci))
Console.WriteLine(msgYear & thisDate.ToString("Y", ci))
Console.WriteLine(msgRoundtripLocal & thisDate.ToString("o"), ci)
Console.WriteLine(msgRoundtripUTC & utcDate.ToString("o"), ci)
Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"), ci)
Console.WriteLine()
EndSub 'Main
EndClass 'Sample
'
'This code example produces the following results:
'
'Standard DateTime Format Specifiers:
'
'This date and time: 4/17/2006 2:29:09 PM
'
'Use ToString(String) and the current thread culture.
'
'Culture: English (United States)
'
'(d) Short date: . . . . . . . 4/17/2006
'(D) Long date:. . . . . . . . Monday, April 17, 2006
'(t) Short time: . . . . . . . 2:29 PM
'(T) Long time:. . . . . . . . 2:29:09 PM
'(f) Full date/short time: . . Monday, April 17, 2006 2:29 PM
'(F) Full date/long time:. . . Monday, April 17, 2006 2:29:09 PM
'(g) General date/short time:. 4/17/2006 2:29 PM
'(G) General date/long time (default):
' . . . . . . . . . . . . . 4/17/2006 2:29:09 PM
'(M) Month:. . . . . . . . . . April 17
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
' . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Monday, April 17, 2006 9:29:09 PM
'(Y) Year: . . . . . . . . . . April, 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture: German (Germany)
'
'(d) Short date: . . . . . . . 17.04.2006
'(D) Long date:. . . . . . . . Montag, 17. April 2006
'(t) Short time: . . . . . . . 14:29
'(T) Long time:. . . . . . . . 14:29:09
'(f) Full date/short time: . . Montag, 17. April 2006 14:29
'(F) Full date/long time:. . . Montag, 17. April 2006 14:29:09
'(g) General date/short time:. 17.04.2006 14:29
'(G) General date/long time (default):
' . . . . . . . . . . . . . 17.04.2006 14:29:09
'(M) Month:. . . . . . . . . . 17 April
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
' . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Montag, 17. April 2006 21:29:09
'(Y) Year: . . . . . . . . . . April 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000