本文整理匯總了VB.NET中System.Convert.ToString方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Convert.ToString方法的具體用法?VB.NET Convert.ToString怎麽用?VB.NET Convert.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Convert
的用法示例。
在下文中一共展示了Convert.ToString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: bases
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
輸出:
Base 2 conversion: -32768 --> 0x1000000000000000 -13621 --> 0x1100101011001011 -18 --> 0x1111111111101110 12 --> 0x1100 19142 --> 0x100101011000110 32767 --> 0x111111111111111 Base 8 conversion: -32768 --> 0x100000 -13621 --> 0x145313 -18 --> 0x177756 12 --> 0x14 19142 --> 0x45306 32767 --> 0x77777 Base 10 conversion: -32768 --> 0x-32768 -13621 --> 0x-13621 -18 --> 0x-18 12 --> 0x12 19142 --> 0x19142 32767 --> 0x32767 Base 16 conversion: -32768 --> 0x8000 -13621 --> 0xcacb -18 --> 0xffee 12 --> 0xc 19142 --> 0x4ac6 32767 --> 0x7fff
示例2: numbers
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
輸出:
-32768 --> ~32768 32767 --> 32767
示例3:
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))
輸出:
18446744073709551615 --> 18446744073709551615
示例4: numbers
' 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
輸出:
1734231911290.16: en-US: 1734231911290.16 fr-FR: 1734231911290,16 ja-JP: 1734231911290.16 ru-RU: 1734231911290,16 -17394.32921: en-US: -17394.32921 fr-FR: -17394,32921 ja-JP: -17394.32921 ru-RU: -17394,32921 3193.23: en-US: 3193.23 fr-FR: 3193,23 ja-JP: 3193.23 ru-RU: 3193,23 98012368321.684: en-US: 98012368321.684 fr-FR: 98012368321,684 ja-JP: 98012368321.684 ru-RU: 98012368321,684
示例5: cultureNames
' 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
輸出:
Converting the date 04/15/2010 20:30:40: en-US: 4/15/2010 8:30:40 PM es-AR: 15/04/2010 08:30:40 p.m. fr-FR: 15/04/2010 20:30:40 hi-IN: 15-04-2010 20:30:40 ja-JP: 2010/04/15 20:30:40 nl-NL: 15-4-2010 20:30:40 ru-RU: 15.04.2010 20:30:40 ur-PK: 15/04/2010 8:30:40 PM
示例6: numbers
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
輸出:
-2147483648 --> ~2147483648 2147483647 --> 2147483647
示例7: numbers
' 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
輸出:
-1.5345E+16: en-US: -1.5345E+16 fr-FR: -1,5345E+16 ja-JP: -1.5345E+16 ru-RU: -1,5345E+16 -123.4321: en-US: -123.4321 fr-FR: -123,4321 ja-JP: -123.4321 ru-RU: -123,4321 19092.123: en-US: 19092.123 fr-FR: 19092,123 ja-JP: 19092.123 ru-RU: 19092,123 1.173423191129E+16: en-US: 1.173423191129E+16 fr-FR: 1,173423191129E+16 ja-JP: 1.173423191129E+16 ru-RU: 1,173423191129E+16
示例8: bases
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
輸出:
Base 2 conversion: -2147483648 --> 0x10000000000000000000000000000000 -19327543 --> 0x11111110110110010001010111001001 -13621 --> 0x11111111111111111100101011001011 -18 --> 0x11111111111111111111111111101110 12 --> 0x1100 19142 --> 0x100101011000110 2147483647 --> 0x1111111111111111111111111111111 Base 8 conversion: -2147483648 --> 0x20000000000 -19327543 --> 0x37666212711 -13621 --> 0x37777745313 -18 --> 0x37777777756 12 --> 0x14 19142 --> 0x45306 2147483647 --> 0x17777777777 Base 10 conversion: -2147483648 --> 0x-2147483648 -19327543 --> 0x-19327543 -13621 --> 0x-13621 -18 --> 0x-18 12 --> 0x12 19142 --> 0x19142 2147483647 --> 0x2147483647 Base 16 conversion: -2147483648 --> 0x80000000 -19327543 --> 0xfed915c9 -13621 --> 0xffffcacb -18 --> 0xffffffee 12 --> 0xc 19142 --> 0x4ac6 2147483647 --> 0x7fffffff
示例9: numbers
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
輸出:
~128 ~12 17 127
示例10: bases
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
輸出:
Base 2 conversion: -9223372036854775808 --> 0x1000000000000000000000000000000000000000000000000000000000000000 -193275430 --> 0x1111111111111111111111111111111111110100011110101101100111011010 -13621 --> 0x1111111111111111111111111111111111111111111111111100101011001011 -18 --> 0x1111111111111111111111111111111111111111111111111111111111101110 12 --> 0x1100 1914206117 --> 0x1110010000110000111011110100101 9223372036854775807 --> 0x111111111111111111111111111111111111111111111111111111111111111 Base 8 conversion: -9223372036854775808 --> 0x1000000000000000000000 -193275430 --> 0x1777777777776436554732 -13621 --> 0x1777777777777777745313 -18 --> 0x1777777777777777777756 12 --> 0x14 1914206117 --> 0x16206073645 9223372036854775807 --> 0x777777777777777777777 Base 10 conversion: -9223372036854775808 --> 0x-9223372036854775808 -193275430 --> 0x-193275430 -13621 --> 0x-13621 -18 --> 0x-18 12 --> 0x12 1914206117 --> 0x1914206117 9223372036854775807 --> 0x9223372036854775807 Base 16 conversion: -9223372036854775808 --> 0x8000000000000000 -193275430 --> 0xfffffffff47ad9da -13621 --> 0xffffffffffffcacb -18 --> 0xffffffffffffffee 12 --> 0xc 1914206117 --> 0x721877a5 9223372036854775807 --> 0x7fffffffffffffff
示例11: Example
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
輸出:
-40.00 °C 0.00 °C 100.00 °C
示例12: Example
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
輸出:
-40.00 °C 273.15 K 100.00 °C
示例13: numbers
' 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
輸出:
-1.5345E+16: en-US: -1.5345E+16 fr-FR: -1,5345E+16 ja-JP: -1.5345E+16 ru-RU: -1,5345E+16 -123.4321: en-US: -123.4321 fr-FR: -123,4321 ja-JP: -123.4321 ru-RU: -123,4321 19092.123: en-US: 19092.123 fr-FR: 19092,123 ja-JP: 19092.123 ru-RU: 19092,123 1.173423191129E+16: en-US: 1.173423191129E+16 fr-FR: 1,173423191129E+16 ja-JP: 1.173423191129E+16 ru-RU: 1,173423191129E+16
示例14: DummyProvider
' 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
示例15:
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))
輸出:
65535 --> 65535