當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Convert.ToSingle方法代碼示例

本文整理匯總了VB.NET中System.Convert.ToSingle方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Convert.ToSingle方法的具體用法?VB.NET Convert.ToSingle怎麽用?VB.NET Convert.ToSingle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Convert的用法示例。


在下文中一共展示了Convert.ToSingle方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Example

' 導入命名空間
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { "123456789", "12345.6789", "12 345,6789", _
                                 "123,456.789", "123 456,789", "123,456,789.0123", _
                                 "123 456 789,0123", "1.235e12", "1.03221e-05", _
                                 Double.MaxValue.ToString() }
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), _
                                        New CultureInfo("fr-FR") } 

      For Each culture As CultureInfo In cultures
         Console.WriteLine("String -> Single Conversion Using the {0} Culture", _
                           culture.Name)
         For Each value As String In values
            Console.Write("{0,22}  ->  ", value)
            Try
               Console.WriteLine(Convert.ToSingle(value, culture))
            Catch e As FormatException
               Console.WriteLine("FormatException")
            CAtch e As OverflowException
               Console.WriteLine("Overflow")
            End Try   
         Next
         Console.WriteLine()
      Next                     
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:29,代碼來源:Convert.ToSingle

輸出:

String -> Single Conversion Using the en-US Culture
123456789  ->  1.234568E+08
12345.6789  ->  12345.68
12 345,6789  ->  FormatException
123,456.789  ->  123456.8
123 456,789  ->  FormatException
123,456,789.0123  ->  1.234568E+08
123 456 789,0123  ->  FormatException
1.235e12  ->  1.235E+12
1.03221e-05  ->  1.03221E-05
1.79769313486232E+308  ->  Overflow

String -> Single Conversion Using the fr-FR Culture
123456789  ->  1.234568E+08
12345.6789  ->  FormatException
12 345,6789  ->  12345.68
123,456.789  ->  FormatException
123 456,789  ->  123456.8
123,456,789.0123  ->  FormatException
123 456 789,0123  ->  1.234568E+08
1.235e12  ->  FormatException
1.03221e-05  ->  FormatException
1.79769313486232E+308  ->  FormatException

示例2: numbers

Dim numbers() As SByte = { SByte.MinValue, -23, 0, 17, SByte.MaxValue }
Dim result As Single

For Each number As SByte In numbers
   result = Convert.ToSingle(number)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                        number.GetType().Name, number, _
                        result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the SByte value '-128' to the Single value -128.
Converted the SByte value '-23' to the Single value -23.
Converted the SByte value '0' to the Single value 0.
Converted the SByte value '17' to the Single value 17.
Converted the SByte value '127' to the Single value 127.

示例3: New

' 導入命名空間
Imports System.Globalization

Public Class Temperature : Implements IConvertible
   Private m_Temp As Single

   Public Sub New(temperature As Single)
      Me.m_Temp = temperature
   End Sub
   
   Public ReadOnly Property Celsius() As Single
      Get
         Return Me.m_Temp
      End Get   
   End Property
   
   Public ReadOnly Property Kelvin() As Single
      Get
         Return Me.m_Temp + 273.15F
      End Get
   End Property
   
   Public ReadOnly Property Fahrenheit() As Single
      Get
         Return CSng(Math.Round(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

   ' IConvertible implementations.
   Public Function GetTypeCode() As TypeCode _
                   Implements IConvertible.GetTypeCode
      Return TypeCode.Object
   End Function
   
   Public Function ToBoolean(provider As IFormatProvider) As Boolean _
                   Implements IConvertible.ToBoolean
      If m_Temp = 0 Then
         Return False
      Else
         Return True
      End If
   End Function 
   
   Public Function ToByte(provider As IFormatProvider) As Byte _
                   Implements IConvertible.ToByte
      If m_Temp < Byte.MinValue Or m_Temp > Byte.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the Byte type.", _ 
                                                   Me.m_Temp)) 
      Else
         Return Convert.ToByte(Me.m_Temp)
      End If       
   End Function
   
   Public Function ToChar(provider As IFormatProvider) As Char _
                   Implements IConvertible.ToChar
      Throw New InvalidCastException("Temperature to Char conversion is not supported.")
   End Function 
   
   Public Function ToDateTime(provider As IFormatProvider) As Date _
                   Implements IConvertible.ToDateTime
      Throw New InvalidCastException("Temperature to DateTime conversion is not supported.")
   End Function
   
   Public Function ToDecimal(provider As IFormatProvider) As Decimal _
                   Implements IConvertible.ToDecimal
      Return Convert.ToDecimal(Me.m_Temp)
   End Function
   
   Public Function ToDouble(provider As IFormatProvider) As Double _
                   Implements IConvertible.ToDouble
      Return Convert.ToDouble(Me.m_Temp)
   End Function   
   
   Public Function ToInt16(provider As IFormatProvider) As Int16 _
                   Implements IConvertible.ToInt16
      If Me.m_Temp < Int16.MinValue Or Me.m_Temp > Int16.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the Int16 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToInt16(Me.m_Temp)   
      End If
   End Function
   
   Public Function ToInt32(provider As IFormatProvider) As Int32 _
                   Implements IConvertible.ToInt32
      If Me.m_Temp < Int32.MinValue Or Me.m_Temp > Int32.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the Int32 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToInt32(Me.m_Temp)
      End If      
   End Function
   
   Public Function ToInt64(provider As IFormatProvider) As Int64 _
                   Implements IConvertible.ToInt64
      If Me.m_Temp < Int64.MinValue Or Me.m_Temp > Int64.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the Int64 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToInt64(Me.m_Temp)
      End If      
   End Function
   
   Public Function ToSByte(provider As IFormatProvider) As SByte _
                   Implements IConvertible.ToSByte
      If Me.m_Temp < SByte.MinValue Or Me.m_Temp > SByte.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the SByte type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToSByte(Me.m_Temp)
      End If      
   End Function

   Public Function ToSingle(provider As IFormatProvider) As Single _
                   Implements IConvertible.ToSingle
      Return Me.m_Temp
   End Function

   Public Overloads Function ToString(provider As IFormatProvider) As String _
                   Implements IConvertible.ToString
      Return m_Temp.ToString("N2", provider) & " °C"
   End Function
   
   Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object _
                   Implements IConvertible.ToType
      Select Case Type.GetTypeCode(conversionType)
         Case TypeCode.Boolean 
            Return Me.ToBoolean(Nothing)
         Case TypeCode.Byte
            Return Me.ToByte(Nothing)
         Case TypeCode.Char
            Return Me.ToChar(Nothing)
         Case TypeCode.DateTime
            Return Me.ToDateTime(Nothing)
         Case TypeCode.Decimal
            Return Me.ToDecimal(Nothing)
         Case TypeCode.Double
            Return Me.ToDouble(Nothing)
         Case TypeCode.Int16
            Return Me.ToInt16(Nothing)
         Case TypeCode.Int32
            Return Me.ToInt32(Nothing)
         Case TypeCode.Int64
            Return Me.ToInt64(Nothing)
         Case TypeCode.Object
            If GetType(Temperature).Equals(conversionType) Then
               Return Me
            Else
               Throw New InvalidCastException(String.Format("Conversion to a {0} is not supported.", _
                                                            conversionType.Name))
            End If 
         Case TypeCode.SByte
            Return Me.ToSByte(Nothing)
         Case TypeCode.Single
            Return Me.ToSingle(Nothing)
         Case TypeCode.String
            Return Me.ToString(provider)
         Case TypeCode.UInt16
            Return Me.ToUInt16(Nothing)
         Case TypeCode.UInt32
            Return Me.ToUInt32(Nothing)
         Case TypeCode.UInt64
            Return Me.ToUInt64(Nothing)   
         Case Else
            Throw New InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name))   
      End Select
   End Function
   
   Public Function ToUInt16(provider As IFormatProvider) As UInt16 _
                   Implements IConvertible.ToUInt16
      If Me.m_Temp < UInt16.MinValue Or Me.m_Temp > UInt16.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the UInt16 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToUInt16(Me.m_Temp)
      End If   
   End Function

   Public Function ToUInt32(provider As IFormatProvider) As UInt32 _
                   Implements IConvertible.ToUInt32
      If Me.m_Temp < UInt32.MinValue Or Me.m_Temp > UInt32.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the UInt32 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToUInt32(Me.m_Temp)
      End If   
   End Function
   
   Public Function ToUInt64(provider As IFormatProvider) As UInt64 _
                   Implements IConvertible.ToUInt64
      If Me.m_Temp < UInt64.MinValue Or Me.m_Temp > UInt64.MaxValue Then
         Throw New OverflowException(String.Format("{0} is out of range of the UInt64 type.", _
                                                   Me.m_Temp))
      Else
         Return Convert.ToUInt64(Me.m_temp)
      End If   
   End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:202,代碼來源:Convert.ToSingle

示例4: Main

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.ToInt32(cold, Nothing))
      Console.WriteLine(Convert.ToInt32(freezing, Nothing))
      Console.WriteLine(Convert.ToDouble(boiling, Nothing))
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:11,代碼來源:Convert.ToSingle

輸出:

-40
0
100

示例5: numbers

Dim numbers() As ULong = { UInt64.MinValue, 121, 12345, UInt64.MaxValue }
Dim result As Single

For Each number As ULong In numbers
   result = Convert.ToSingle(number)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                        number.GetType().Name, number, _
                        result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the UInt64 value '0' to the Single value 0.
Converted the UInt64 value '121' to the Single value 121.
Converted the UInt64 value '12345' to the Single value 12345.
Converted the UInt64 value '18446744073709551615' to the Single value 1.844674E+19.

示例6: numbers

Dim numbers() As UInteger = { UInt32.MinValue, 121, 12345, UInt32.MaxValue }
   Dim result As Single
   
   For Each number As UInteger In numbers
      result = Convert.ToSingle(number)
         Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                           number.GetType().Name, number, _
                           result.GetType().Name, result)
   Next   
   ' The example displays the following output:
'    Converted the UInt32 value '0' to the Single value 0.
'    Converted the UInt32 value '121' to the Single value 121.
'    Converted the UInt32 value '12345' to the Single value 12345.
'    Converted the UInt32 value '4294967295' to the Single value 4.294967E+09.
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:14,代碼來源:Convert.ToSingle

示例7: values

Dim values() As String = { "-1,035.77219", "1AFF", "1e-35", "1.63f",
                           "1,635,592,999,999,999,999,999,999", "-17.455",
                           "190.34001", "1.29e325"}
Dim result As Single

For Each value As String In values
   Try
      result = Convert.ToSingle(value)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                        value.GetType().Name, value, _
                        result.GetType().Name, result)
   Catch e As FormatException
      Console.WriteLine("Unable to convert '{0}' to a Single.", value)            
   Catch e As OverflowException
      Console.WriteLine("'{0}' is outside the range of a Single.", value)
   End Try
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:17,代碼來源:Convert.ToSingle

輸出:

Converted the String value '-1,035.77219' to the Single value -1035.772.
Unable to convert '1AFF' to a Single.
Converted the String value '1e-35' to the Single value 1E-35.
Unable to convert '1.63f' to a Single.
Converted the String value '1,635,592,999,999,999,999,999,999' to the Single value 1.635593E+24.
Converted the String value '-17.455' to the Single value -17.455.
Converted the String value '190.34001' to the Single value 190.34.
1.29e325' is outside the range of a Single.

示例8: values

Dim values() As Object = { True, "a"c, 123, 1.764e32, "9.78", "1e-02", _
                           1.67e03, "A100", "1,033.67", Date.Now, _
                           Decimal.MaxValue }   
Dim result As Single

For Each value As Object In values
   Try
      result = Convert.ToSingle(value)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                        value.GetType().Name, value, _
                        result.GetType().Name, result)

   Catch e As FormatException
      Console.WriteLine("The {0} value {1} is not recognized as a valid Single value.", _
                        value.GetType().Name, value)
   Catch e As OverflowException
      Console.WriteLine("The {0} value {1} is outside the range of the Single type.", _
                        value.GetType().Name, value)
   
   Catch e As InvalidCastException
      Console.WriteLine("Conversion of the {0} value {1} to a Single is not supported.", _
                        value.GetType().Name, value)
   End Try                     
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:24,代碼來源:Convert.ToSingle

輸出:

Converted the Boolean value 'True' to the Single value 1.
Conversion of the Char value a to a Single is not supported.
Converted the Int32 value '123' to the Single value 123.
Converted the Double value '1.764E+32' to the Single value 1.764E+32.
Converted the String value '9.78' to the Single value 9.78.
Converted the String value '1e-02' to the Single value 0.01.
Converted the Double value '1670' to the Single value 1670.
The String value A100 is not recognized as a valid Single value.
Converted the String value '1,033.67' to the Single value 1033.67.
Conversion of the DateTime value 11/7/2008 07:56:24 AM to a Single is not supported.
Converted the Decimal value '79228162514264337593543950335' to the Single value 7.922816E+28.

示例9: numbers

Dim numbers() As UShort = { UInt16.MinValue, 121, 12345, UInt16.MaxValue }
Dim result As Single

For Each number As UShort In numbers
   result = Convert.ToSingle(number)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                        number.GetType().Name, number, _
                        result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the UInt16 value '0' to the Single value 0.
Converted the UInt16 value '121' to the Single value 121.
Converted the UInt16 value '12345' to the Single value 12345.
Converted the UInt16 value '65535' to the Single value 65535.

示例10: numbers

Dim numbers() As Integer = { Int32.MinValue, -1000, 0, 1000, Int32.MaxValue }
Dim result As Single

For Each number As Integer In numbers
   result = Convert.ToSingle(number)
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                     number.GetType().Name, number, _
                     result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the Int32 value '-2147483648' to the Single value -2.147484E+09.
Converted the Int32 value '-1000' to the Single value -1000.
Converted the Int32 value '0' to the Single value 0.
Converted the Int32 value '1000' to the Single value 1000.
Converted the Int32 value '2147483647' to the Single value 2.147484E+09.

示例11: numbers

Dim numbers() As Short = { Int16.MinValue, -1032, 0, 192, Int16.MaxValue }
Dim result As Single

For Each number As Short In numbers
   result = Convert.ToSingle(number)
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                     number.GetType().Name, number, _
                     result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the Int16 value '-32768' to the Single value -32768.
Converted the Int16 value '-1032' to the Single value -1032.
Converted the Int16 value '0' to the Single value 0.
Converted the Int16 value '192' to the Single value 192.
Converted the Int16 value '32767' to the Single value 32767.

示例12: values

Dim values() As Double = { Double.MinValue, -1.38e10, -1023.299, -12.98, _
                           0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }
Dim result As Single

For Each value As Double In values
   result = Convert.ToSingle(value)
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                     value.GetType().Name, value, _
                     result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:10,代碼來源:Convert.ToSingle

輸出:

Converted the Double value '-1.79769313486232E+308' to the Single value -Infinity.
Converted the Double value '-13800000000' to the Single value -1.38E+10.
Converted the Double value '-1023.299' to the Single value -1023.299.
Converted the Double value '-12.98' to the Single value -12.98.
Converted the Double value '0' to the Single value 0.
Converted the Double value '9.113E-16' to the Single value 9.113E-16.
Converted the Double value '103.919' to the Single value 103.919.
Converted the Double value '17834.191' to the Single value 17834.19.
Converted the Double value '1.79769313486232E+308' to the Single value Infinity.

示例13: values

Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _
                            199.55d, 9214.16d, Decimal.MaxValue }
Dim result As Single

For Each value As Decimal In values
   result = Convert.ToSingle(value)
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                     value.GetType().Name, value, _
                     result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:10,代碼來源:Convert.ToSingle

輸出:

Converted the Decimal value '-79228162514264337593543950335' to the Single value -7.922816E+28.
Converted the Decimal value '-1034.23' to the Single value -1034.23.
Converted the Decimal value '-12' to the Single value -12.
Converted the Decimal value '0' to the Single value 0.
Converted the Decimal value '147' to the Single value 147.
Converted the Decimal value '199.55' to the Single value 199.55.
Converted the Decimal value '9214.16' to the Single value 9214.16.
Converted the Decimal value '79228162514264337593543950335' to the Single value 7.922816E+28.

示例14: numbers

Dim numbers() As Byte = { Byte.MinValue, 10, 100, Byte.MaxValue }
Dim result As Single

For Each number As Byte In numbers
   result = Convert.ToSingle(number)
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
                     number.GetType().Name, number, _
                     result.GetType().Name, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源:Convert.ToSingle

輸出:

Converted the Byte value 0 to the Single value 0.
Converted the Byte value 10 to the Single value 10.
Converted the Byte value 100 to the Single value 100.
Converted the Byte value 255 to the Single value 255.

示例15: flags

Dim flags() As Boolean = { True, False }
Dim result As Single

For Each flag As Boolean In flags
   result = Convert.ToSingle(flag)
   Console.WriteLine("Converted {0} to {1}.", flag, result)
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:7,代碼來源:Convert.ToSingle

輸出:

Converted True to 1.
Converted False to 0.


注:本文中的System.Convert.ToSingle方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。