本文整理汇总了VB.NET中System.IFormatProvider接口的典型用法代码示例。如果您正苦于以下问题:VB.NET IFormatProvider接口的具体用法?VB.NET IFormatProvider怎么用?VB.NET IFormatProvider使用的例子?那么恭喜您, 这里精选的接口代码示例或许可以为您提供帮助。
在下文中一共展示了IFormatProvider接口的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Main
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateValue As Date = #06/01/2009 4:37PM#
Dim cultures() As CultureInfo = {New CultureInfo("en-US"), _
New CultureInfo("fr-FR"), _
New CultureInfo("it-IT"), _
New CultureInfo("de-DE") }
For Each culture As CultureInfo In cultures
Console.WriteLine("{0}: {1}", culture.Name, dateValue.ToString(culture))
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:15,代码来源:IFormatProvider 输出:
en-US: 6/1/2009 4:37:00 PM
fr-FR: 01/06/2009 16:37:00
it-IT: 01/06/2009 16.37.00
de-DE: 01.06.2009 16:37:00
示例2: GetFormat
Public Class AcctNumberFormat : Implements IFormatProvider, ICustomFormatter
Private Const ACCT_LENGTH As Integer = 12
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, arg As Object, formatProvider As IFormatProvider) As String _
Implements ICustomFormatter.Format
' Provide default formatting if arg is not an Int64.
If Not TypeOf arg Is Int64 Then
Try
Return HandleOtherFormats(fmt, arg)
Catch e As FormatException
Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e)
End Try
End If
' Provider default formatting for unsupported format strings.
Dim ufmt As String = fmt.ToUpper(CultureInfo.InvariantCulture)
If Not (ufmt = "H" Or ufmt = "I") Then
Try
Return HandleOtherFormats(fmt, arg)
Catch e As FormatException
Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e)
End Try
End If
' Convert argument to a string.
Dim result As String = arg.ToString()
' If account number is less than 12 characters, pad with leading zeroes.
If result.Length < ACCT_LENGTH Then result = result.PadLeft(ACCT_LENGTH, "0"c)
' If account number is more than 12 characters, truncate to 12 characters.
If result.Length > ACCT_LENGTH Then result = Left(result, ACCT_LENGTH)
If ufmt = "I" ' Integer-only format.
Return result
' Add hyphens for H format specifier.
Else ' Hypenated format.
Return Left(result, 5) & "-" & Mid(result, 6, 3) & "-" & Right(result, 4)
End If
End Function
Private Function HandleOtherFormats(fmt As String, arg As Object) As String
If TypeOf arg Is IFormattable Then
Return DirectCast(arg, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
ElseIf arg IsNot Nothing Then
Return arg.ToString()
Else
Return String.Empty
End If
End Function
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:61,代码来源:IFormatProvider
示例3: Main
' 导入命名空间
Imports System.Globalization
Public Enum DaysOfWeek As Long
Monday = 1
Tuesday = 2
End Enum
Module TestFormatting
Public Sub Main()
Dim acctNumber As Long, balance As Double
Dim wday As DaysOfWeek
Dim output As String
acctNumber = 104254567890
balance = 16.34
wday = DaysOfWeek.Monday
output = String.Format(New AcctNumberFormat(), "On {2}, the balance of account {0:H} was {1:C2}.", acctNumber, balance, wday)
Console.WriteLine(output)
wday = DaysOfWeek.Tuesday
output = String.Format(New AcctNumberFormat(), "On {2}, the balance of account {0:I} was {1:C2}.", acctNumber, balance, wday)
Console.WriteLine(output)
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:26,代码来源:IFormatProvider 输出:
On Monday, the balance of account 10425-456-7890 was $16.34.
On Tuesday, the balance of account 104254567890 was $16.34.
注:本文中的System.IFormatProvider接口示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论