本文整理汇总了VB.NET中System.Convert.ToDecimal方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Convert.ToDecimal方法的具体用法?VB.NET Convert.ToDecimal怎么用?VB.NET Convert.ToDecimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Convert
的用法示例。
在下文中一共展示了Convert.ToDecimal方法的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" }
Dim cultures() As CultureInfo = { New CultureInfo("en-US"), _
New CultureInfo("fr-FR") }
For Each culture As CultureInfo In cultures
Console.WriteLine("String -> Decimal Conversion Using the {0} Culture", _
culture.Name)
For Each value As String In values
Console.Write("{0,20} -> ", value)
Try
Console.WriteLine(Convert.ToDecimal(value, culture))
Catch e As FormatException
Console.WriteLine("FormatException")
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
输出:
String -> Decimal Conversion Using the en-US Culture 123456789 -> 123456789 12345.6789 -> 12345.6789 12 345,6789 -> FormatException 123,456.789 -> 123456.789 123 456,789 -> FormatException 123,456,789.0123 -> 123456789.0123 123 456 789,0123 -> FormatException String -> Decimal Conversion Using the fr-FR Culture 123456789 -> 123456789 12345.6789 -> FormatException 12 345,6789 -> 12345.6789 123,456.789 -> FormatException 123 456,789 -> 123456.789 123,456,789.0123 -> FormatException 123 456 789,0123 -> 123456789.0123
示例2: New
' 导入命名空间
Imports System.Globalization
Public Class Temperature : Implements IConvertible
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
' 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 Decimal.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 Me.m_Temp
End Function
Public Function ToDouble(provider As IFormatProvider) As Double _
Implements IConvertible.ToDouble
Return Decimal.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 Decimal.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 Decimal.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 Decimal.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 Decimal.ToSByte(Me.m_Temp)
End If
End Function
Public Function ToSingle(provider As IFormatProvider) As Single _
Implements IConvertible.ToSingle
Return Decimal.ToSingle(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 Decimal.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 Decimal.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 Decimal.ToUInt64(Me.m_temp)
End If
End Function
End Class
示例3: Example
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.ToDecimal(cold, Nothing))
Console.WriteLine(Convert.ToDecimal(freezing, Nothing))
Console.WriteLine(Convert.ToDecimal(boiling, Nothing))
End Sub
End Module
输出:
-40 0 100
示例4: numbers
Dim numbers() As ULong = { UInt64.MinValue, 121, 12345, UInt64.MaxValue }
Dim result As Decimal
For Each number As ULong In numbers
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the UInt64 value {0} to {1}.", _
number, result)
Next
输出:
Converted the UInt64 value 0 to 0. Converted the UInt64 value 121 to 121. Converted the UInt64 value 12345 to 12345. Converted the UInt64 value 18446744073709551615 to 18446744073709551615.
示例5: numbers
Dim numbers() As UInteger = { UInt32.MinValue, 121, 12345, UInt32.MaxValue }
Dim result As Decimal
For Each number As UInteger In numbers
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the UInt32 value {0} to {1}.", _
number, result)
Next
输出:
Converted the UInt32 value 0 to 0. Converted the UInt32 value 121 to 121. Converted the UInt32 value 12345 to 12345. Converted the UInt32 value 4294967295 to 4294967295.
示例6: numbers
Dim numbers() As UShort = { UInt16.MinValue, 121, 12345, UInt16.MaxValue }
Dim result As Decimal
For Each number As UShort In numbers
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the UInt16 value {0} to {1}.", _
number, result)
Next
输出:
Converted the UInt16 value 0 to 0. Converted the UInt16 value 121 to 121. Converted the UInt16 value 12345 to 12345. Converted the UInt16 value 65535 to 65535.
示例7: ConvertStringDecimal
Public Sub ConvertStringDecimal(ByVal stringVal As String)
Dim decimalVal As Decimal = 0
Try
decimalVal = System.Convert.ToDecimal(stringVal)
System.Console.WriteLine("The string as a decimal is {0}.", _
decimalVal)
Catch exception As System.OverflowException
System.Console.WriteLine( _
"Overflow in string-to-decimal conversion.")
Catch exception As System.FormatException
System.Console.WriteLine( _
"The string is not formatted as a decimal.")
Catch exception As System.ArgumentException
System.Console.WriteLine("The string is null.")
End Try
' Decimal to string conversion will not overflow.
stringVal = System.Convert.ToString(decimalVal)
System.Console.WriteLine("The decimal as a string is {0}.", _
stringVal)
End Sub
示例8: numbers
Dim numbers() As SByte = { SByte.MinValue, -23, 0, 17, SByte.MaxValue }
Dim result As Decimal
For Each number As SByte In numbers
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the SByte value {0} to {1}.", number, result)
Next
' Converted the SByte value -128 to -128.
' Converted the SByte value -23 to -23.
' Converted the SByte value 0 to 0.
' Converted the SByte value 17 to 17.
' Converted the SByte value 127 to 127.
示例9: values
Dim values() As Object = { True, "a"c, 123, 1.764e32, "9.78", "1e-02", _
1.67e03, "A100", "1,033.67", Date.Now, _
Double.MaxValue }
Dim result As Decimal
For Each value As Object In values
Try
result = Convert.ToDecimal(value)
Console.WriteLine("Converted the {0} value {1} to {2}.", _
value.GetType().Name, value, result)
Catch e As OverflowException
Console.WriteLine("The {0} value {1} is out of range of the Decimal type.", _
value.GetType().Name, value)
Catch e As FormatException
Console.WriteLine("The {0} value {1} is not recognized as a valid Decimal value.", _
value.GetType().Name, value)
Catch e As InvalidCastException
Console.WriteLine("Conversion of the {0} value {1} to a Decimal is not supported.", _
value.GetType().Name, value)
End Try
Next
输出:
Converted the Boolean value True to 1. Conversion of the Char value a to a Decimal is not supported. Converted the Int32 value 123 to 123. The Double value 1.764E+32 is out of range of the Decimal type. Converted the String value 9.78 to 9.78. The String value 1e-02 is not recognized as a valid Decimal value. Converted the Double value 1670 to 1670. The String value A100 is not recognized as a valid Decimal value. Converted the String value 1,033.67 to 1033.67. Conversion of the DateTime value 10/15/2008 05:40:42 PM to a Decimal is not supported. The Double value 1.79769313486232E+308 is out of range of the Decimal type.
示例10: numbers
Dim numbers() As Single = { Single.MinValue, -3e10, -1093.54, 0, 1e-03, _
1034.23, Single.MaxValue }
Dim result As Decimal
For Each number As Single In numbers
Try
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the Single value {0} to {1}.", number, result)
Catch e As OverflowException
Console.WriteLine("{0} is out of range of the Decimal type.", number)
End Try
Next
输出:
-3.402823E+38 is out of range of the Decimal type. Converted the Single value -3E+10 to -30000000000. Converted the Single value -1093.54 to -1093.54. Converted the Single value 0 to 0. Converted the Single value 0.001 to 0.001. Converted the Single value 1034.23 to 1034.23. 3.402823E+38 is out of range of the Decimal type.
示例11:
Console.WriteLine(Convert.ToDecimal(1234567500.12f)) ' Displays 1234568000
Console.WriteLine(Convert.ToDecimal(1234568500.12f)) ' Displays 1234568000
Console.WriteLine(Convert.ToDecimal(10.980365f)) ' Displays 10.98036
Console.WriteLine(Convert.ToDecimal(10.980355f)) ' Displays 10.98036
示例12: numbers
Dim numbers() As Integer = { Int32.MinValue, -1000, 0, 1000, Int32.MaxValue }
Dim result As Decimal
For Each number As Integer In numbers
result = Convert.ToDecimal(number)
Console.WriteLine("Converted the Int32 value {0} to the Decimal value {1}.", _
number, result)
Next
输出:
Converted the Int32 value -2147483648 to the Decimal value -2147483648. Converted the Int32 value -1000 to the Decimal value -1000. Converted the Int32 value 0 to the Decimal value 0. Converted the Int32 value 1000 to the Decimal value 1000. Converted the Int32 value 2147483647 to the Decimal value 2147483647.
示例13: flags
Dim flags() As Boolean = { True, False }
Dim result As Decimal
For Each flag As Boolean In flags
result = Convert.ToDecimal(flag)
Console.WriteLine("Converted {0} to {1}.", flag, result)
Next
输出:
Converted True to 1. Converted False to 0.
示例14: ConvertByteDecimal
Public Sub ConvertByteDecimal(ByVal byteVal As Byte)
Dim decimalVal As Decimal
' Byte to decimal conversion will not overflow.
decimalVal = System.Convert.ToDecimal(byteVal)
System.Console.WriteLine("The byte as a decimal is {0}.", _
decimalVal)
' Decimal to byte conversion can overflow.
Try
byteVal = System.Convert.ToByte(decimalVal)
System.Console.WriteLine("The Decimal as a byte is {0}.", _
byteVal)
Catch exception As System.OverflowException
System.Console.WriteLine( _
"Overflow in decimal-to-byte conversion.")
End Try
End Sub
示例15: ConvertCharDecimal
Public Sub ConvertCharDecimal(ByVal charVal As Char)
Dim decimalVal As [Decimal] = 0
' Char to decimal conversion is not supported and will always
' throw an InvalidCastException.
Try
decimalVal = System.Convert.ToDecimal(charVal)
Catch exception As System.InvalidCastException
System.Console.WriteLine( _
"Char-to-Decimal conversion is not supported " + _
"by the .NET Framework.")
End Try
'Decimal to char conversion is also not supported.
Try
charVal = System.Convert.ToChar(decimalVal)
Catch exception As System.InvalidCastException
System.Console.WriteLine( _
"Decimal-to-Char conversion is not supported " + _
"by the .NET Framework.")
End Try
End Sub