作者:VB.NET开发
项目:Syste
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Short = { Int16.MinValue, -13621, -18, 12, 19142, _
Int16.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Short In numbers
Console.WriteLine(" {0,-8} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
作者:VB.NET开发
项目:Syste
Dim numbers() As Short = { Int16.MinValue, Int16.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Short In numbers
Console.WriteLine("{0,-8} --> {1,8}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
作者:VB.NET开发
项目:Syste
Dim number As ULong = UInt64.MaxValue
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
Console.WriteLine("{0,-12} --> {1,12}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
作者:VB.NET开发
项目:Syste
' Define an array of numbers to display.
Dim numbers() As Decimal = { 1734231911290.16d, -17394.32921d, _
3193.23d, 98012368321.684d }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Decimal In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
作者:VB.NET开发
项目:Syste
' Specify the date to be formatted using various cultures.
Dim tDate As New Date(2010, 4, 15, 20, 30, 40, 333)
' Specify the cultures.
Dim cultureNames() As String = { "en-US", "es-AR", "fr-FR", "hi-IN", _
"ja-JP", "nl-NL", "ru-RU", "ur-PK" }
Console.WriteLine("Converting the date {0}: ", _
Convert.ToString(tDate, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In CultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Dim dateString As String = Convert.ToString(tDate, culture)
Console.WriteLine(" {0}: {1,-12}", _
culture.Name, dateString)
Next
作者:VB.NET开发
项目:Syste
Dim numbers() As Integer = { Int32.MinValue, Int32.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Integer In numbers
Console.WriteLine("{0,-12} --> {1,12}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
作者:VB.NET开发
项目:Syste
' Define an array of numbers to display.
Dim numbers() As Double = { -1.5345e16, -123.4321, 19092.123, _
1.1734231911290e16 }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Double In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
作者:VB.NET开发
项目:Syste
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Integer = { Int32.MinValue, -19327543, -13621, -18, 12, _
19142, Int32.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Integer In numbers
Console.WriteLine(" {0,-15} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
作者:VB.NET开发
项目:Syste
Dim numbers() As SByte = { SByte.MinValue, -12, 17, SByte.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As SByte In numbers
Console.WriteLine(Convert.ToString(number, nfi))
Next
作者:VB.NET开发
项目:Syste
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Long = { Int64.MinValue, -193275430, -13621, -18, 12, _
1914206117, Int64.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Long In numbers
Console.WriteLine(" {0,-23} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
作者:VB.NET开发
项目:Syste
Public Class Temperature
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius() As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin() As Decimal
Get
Return Me.m_Temp + 273.15d
End Get
End Property
Public ReadOnly Property Fahrenheit() As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return m_Temp.ToString("N2") & " °C"
End Function
End Class
Module Example
Public Sub Main()
Dim cold As New Temperature(-40)
Dim freezing As New Temperature(0)
Dim boiling As New Temperature(100)
Console.WriteLine(Convert.ToString(cold, Nothing))
Console.WriteLine(Convert.ToString(freezing, Nothing))
Console.WriteLine(Convert.ToString(boiling, Nothing))
End Sub
End Module
作者:VB.NET开发
项目:Syste
Public Class Temperature : Implements IFormattable
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin As Decimal
Get
Return Me.m_Temp + 273.15d
End Get
End Property
Public ReadOnly Property Fahrenheit As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return ToString("G", Nothing)
End Function
Public Overloads Function ToString(fmt As String,
provider As IFormatProvider) As String _
Implements IFormattable.ToString
Dim formatter As TemperatureProvider = Nothing
If provider IsNot Nothing Then formatter = TryCast(provider.GetFormat(GetType(TemperatureProvider)),
TemperatureProvider)
If String.IsNullOrWhiteSpace(fmt) Then
If formatter IsNot Nothing Then
fmt = formatter.Format
Else
fmt = "G"
End If
End If
Select Case fmt.ToUpper()
Case "G", "C"
Return m_Temp.ToString("N2") & " °C"
Case "F"
Return Fahrenheit.ToString("N2") + " °F"
Case "K"
Return Kelvin.ToString("N2") + " K"
Case Else
Throw New FormatException(String.Format("'{0}' is not a valid format specifier.", fmt))
End Select
End Function
End Class
Public Class TemperatureProvider : Implements IFormatProvider
Private fmtStrings() As String = { "C", "G", "F", "K" }
Private rnd As New Random()
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
Return Me
End Function
Public ReadOnly Property Format As String
Get
Return fmtStrings(rnd.Next(0, fmtStrings.Length))
End Get
End Property
End Class
Module Example
Public Sub Main()
Dim cold As New Temperature(-40)
Dim freezing As New Temperature(0)
Dim boiling As New Temperature(100)
Dim tp As New TemperatureProvider()
Console.WriteLine(Convert.ToString(cold, tp))
Console.WriteLine(Convert.ToString(freezing, tp))
Console.WriteLine(Convert.ToString(boiling, tp))
End Sub
End Module
作者:VB.NET开发
项目:Syste
' Define an array of numbers to display.
Dim numbers() As Single = { -1.5345e16, -123.4321, 19092.123, _
1.1734231911290e16 }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Single In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
作者:VB.NET开发
项目:Syste
' Example of Convert.ToString( non-numeric types, IFormatProvider ).
Imports System.Globalization
' An instance of this class can be passed to methods that require
' an IFormatProvider.
Public Class DummyProvider
Implements IFormatProvider
' Normally, GetFormat returns an object of the requested type
' (usually itself) if it is able; otherwise, it returns Nothing.
Public Function GetFormat( argType As Type ) As Object _
Implements IFormatProvider.GetFormat
' Here, the type of argType is displayed, and GetFormat
' always returns Nothing.
Console.Write( "{0,-40}", argType.ToString( ) )
Return Nothing
End Function
End Class
Module ConvertNonNumericProviderDemo
Sub Main( )
' Create an instance of the IFormatProvider.
Dim provider As New DummyProvider( )
Dim converted As String
' Convert these values using DummyProvider.
Dim Int32A As Integer = -252645135
Dim DoubleA As Double = 61680.3855
Dim ObjDouble As Object = CType( -98765.4321, Object )
Dim DayTimeA As DateTime = _
new DateTime( 2001, 9, 11, 13, 45, 0 )
Dim BoolA As Boolean = True
Dim StringA As String = "Qwerty"
Dim CharA As Char = "$"c
Dim TSpanA As TimeSpan = New TimeSpan( 0, 18, 0 )
Dim ObjOther As Object = CType( provider, Object )
Console.WriteLine( "This example of " & _
"Convert.ToString( non-numeric, IFormatProvider ) " & _
vbCrLf & "generates the following output. The " & _
"provider type, argument type, " & vbCrLf & "and " & _
"argument value are displayed." )
Console.WriteLine( vbCrLf & _
"Note: The IFormatProvider object is not called for " & _
"Boolean, String, " & vbCrLf & "Char, TimeSpan, " & _
"and non-numeric Object." )
' The format provider is called for these conversions.
Console.WriteLine( )
converted = Convert.ToString( Int32A, provider )
Console.WriteLine( "Int32 {0}", converted )
converted = Convert.ToString( DoubleA, provider )
Console.WriteLine( "Double {0}", converted )
converted = Convert.ToString( ObjDouble, provider )
Console.WriteLine( "Object {0}", converted )
converted = Convert.ToString( DayTimeA, provider )
Console.WriteLine( "DateTime {0}", converted )
' The format provider is not called for these conversions.
Console.WriteLine( )
converted = Convert.ToString( BoolA, provider )
Console.WriteLine( "Boolean {0}", converted )
converted = Convert.ToString( StringA, provider )
Console.WriteLine( "String {0}", converted )
converted = Convert.ToString( CharA, provider )
Console.WriteLine( "Char {0}", converted )
converted = Convert.ToString( TSpanA, provider )
Console.WriteLine( "TimeSpan {0}", converted )
converted = Convert.ToString( ObjOther, provider )
Console.WriteLine( "Object {0}", converted )
End Sub
End Module
' This example of Convert.ToString( non-numeric, IFormatProvider )
' generates the following output. The provider type, argument type,
' and argument value are displayed.
'
' Note: The IFormatProvider object is not called for Boolean, String,
' Char, TimeSpan, and non-numeric Object.
'
' System.Globalization.NumberFormatInfo Int32 -252645135
' System.Globalization.NumberFormatInfo Double 61680.3855
' System.Globalization.NumberFormatInfo Object -98765.4321
' System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM
'
' Boolean True
' String Qwerty
' Char $
' TimeSpan 00:18:00
' Object DummyProvider
作者:VB.NET开发
项目:Syste
Dim number As UShort = UInt16.MaxValue
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
Console.WriteLine("{0,-6} --> {1,6}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
作者:VB.NET开发
项目:Syste
Dim number As UInteger = UInt32.MaxValue
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
Console.WriteLine("{0,-8} --> {1,8}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
作者:VB.NET开发
项目:Syste
Dim numbers() As Long = { CLng(Int32.MinValue) * 2, CLng(Int32.MaxValue) * 2 }
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Long In numbers
Console.WriteLine("{0,-12} --> {1,12}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
作者:VB.NET开发
项目:Syste
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Byte = { Byte.MinValue, 12, 103, Byte.MaxValue}
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Byte In numbers
Console.WriteLine(" {0,-5} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
作者:VB.NET开发
项目:Syste
Dim numbers() As ULong = { UInt64.MinValue, 1031, 189045, UInt64.MaxValue }
Dim result As String
For Each number As ULong In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
作者:VB.NET开发
项目:Syste
' Define an array of numbers to display.
Dim numbers() As Byte = { 12, 100, Byte.MaxValue }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR" }
For Each number As Byte In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next