本文整理匯總了VB.NET中System.Decimal.Decimal構造函數的典型用法代碼示例。如果您正苦於以下問題:VB.NET Decimal構造函數的具體用法?VB.NET Decimal怎麽用?VB.NET Decimal使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在類System.Decimal
的用法示例。
在下文中一共展示了Decimal構造函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: DecimalCtorDoDemo
' Example of the Decimal( Double ) constructor.
Module DecimalCtorDoDemo
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
' Create a Decimal object and display its value.
Sub CreateDecimal( value As Double, valToStr As String )
' Format and display the constructor.
Console.Write( "{0,-34}", _
String.Format( "Decimal( {0} )", valToStr ) )
' Construct the Decimal value.
Try
Dim decimalNum As New Decimal( value )
' Display the value if it was created successfully.
Console.WriteLine( "{0,31}", decimalNum )
' Display the exception type if an exception was thrown.
Catch ex As Exception
Console.WriteLine( "{0,31}", GetExceptionType( ex ) )
End Try
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( Double ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-34}{1,31}", "Constructor", _
"Value or Exception" )
Console.WriteLine( "{0,-34}{1,31}", "-----------", _
"------------------" )
' Construct Decimal objects from Double values.
CreateDecimal( 1.23456789E+5, "1.23456789E+5" )
CreateDecimal( 1.234567890123E+15, "1.234567890123E+15" )
CreateDecimal( 1.2345678901234567E+25, _
"1.2345678901234567E+25" )
CreateDecimal( 1.2345678901234567E+35, _
"1.2345678901234567E+35" )
CreateDecimal( 1.23456789E-5, "1.23456789E-5" )
CreateDecimal( 1.234567890123E-15, "1.234567890123E-15" )
CreateDecimal( 1.2345678901234567E-25, _
"1.2345678901234567E-25" )
CreateDecimal( 1.2345678901234567E-35, _
"1.2345678901234567E-35" )
CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" )
End Sub
End Module
' This example of the Decimal( Double ) constructor
輸出:
Constructor Value or Exception ----------- ------------------ Decimal( 1.23456789E+5 ) 123456.789 Decimal( 1.234567890123E+15 ) 1234567890123000 Decimal( 1.2345678901234567E+25 ) 12345678901234600000000000 Decimal( 1.2345678901234567E+35 ) OverflowException Decimal( 1.23456789E-5 ) 0.0000123456789 Decimal( 1.234567890123E-15 ) 0.000000000000001234567890123 Decimal( 1.2345678901234567E-25 ) 0.0000000000000000000000001235 Decimal( 1.2345678901234567E-35 ) 0 Decimal( 1.0 / 7.0 ) 0.142857142857143
示例2: DecimalCtorIDemo
' Example of the Decimal( Integer ) constructor.
Module DecimalCtorIDemo
' Create a Decimal object and display its value.
Sub CreateDecimal( value As Integer, valToStr As String )
Dim decimalNum As New Decimal( value )
' Format the constructor for display.
Dim ctor As String = _
String.Format( "Decimal( {0} )", valToStr )
' Display the constructor and its value.
Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( Integer ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" )
Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" )
' Construct Decimal objects from Integer values.
CreateDecimal( Integer.MinValue, "Integer.MinValue" )
CreateDecimal( Integer.MaxValue, "Integer.MaxValue" )
CreateDecimal( 0, "0" )
CreateDecimal( 999999999, "999999999" )
CreateDecimal( &H40000000, "&H40000000" )
CreateDecimal( &HC0000000, "&HC0000000" )
End Sub
End Module
' This example of the Decimal( Integer ) constructor
輸出:
Constructor Value ----------- ----- Decimal( Integer.MinValue ) -2147483648 Decimal( Integer.MaxValue ) 2147483647 Decimal( 0 ) 0 Decimal( 999999999 ) 999999999 Decimal( &H40000000 ) 1073741824 Decimal( &HC0000000 ) -1073741824
示例3: DecimalCtorIArrDemo
' Example of the Decimal( Integer( ) ) constructor.
Module DecimalCtorIArrDemo
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
' Create a Decimal object and display its value.
Sub CreateDecimal( ByVal bits( ) As Integer )
' Format and save the constructor.
Dim ctor As String = String.Format( "Decimal( {{ &H{0:X}", bits( 0 ) )
Dim valOrExc As String
Dim index As Integer
For index = 1 to bits.Length - 1
ctor &= String.Format( ", &H{0:X}", bits( index ) )
Next index
ctor &= " } )"
' Construct the Decimal value.
Try
Dim decimalNum As New Decimal( bits )
' Format the Decimal value for display.
valOrExc = decimalNum.ToString( )
' Save the exception type if an exception was thrown.
Catch ex As Exception
valOrExc = GetExceptionType( ex )
End Try
' Display the constructor and Decimal value or exception.
Dim ctorLen As Integer = 76 - valOrExc.Length
If ctorLen > ctor.Length Then
' Display the data on one line if it will fit.
Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), _
valOrExc )
' Otherwise, display the data on two lines.
Else
Console.WriteLine( "{0}", ctor )
Console.WriteLine( "{0,76}", valOrExc )
End If
End Sub
Sub Main( )
Console.WriteLine( "This example of the " & _
"Decimal( Integer( ) ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-38}{1,38}", "Constructor", _
"Value or Exception" )
Console.WriteLine( "{0,-38}{1,38}", "-----------", _
"------------------" )
' Construct Decimal objects from Integer arrays.
CreateDecimal( New Integer( ) { 0, 0, 0, 0 } )
CreateDecimal( New Integer( ) { 0, 0, 0 } )
CreateDecimal( New Integer( ) { 0, 0, 0, 0, 0 } )
CreateDecimal( New Integer( ) { 1000000000, 0, 0, 0 } )
CreateDecimal( New Integer( ) { 0, 1000000000, 0, 0 } )
CreateDecimal( New Integer( ) { 0, 0, 1000000000, 0 } )
CreateDecimal( New Integer( ) { 0, 0, 0, 1000000000 } )
CreateDecimal( New Integer( ) { -1, -1, -1, 0 } )
CreateDecimal( New Integer( ) { -1, -1, -1, &H80000000 } )
CreateDecimal( New Integer( ) { -1, 0, 0, &H100000 } )
CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0000 } )
CreateDecimal( New Integer( ) { -1, 0, 0, &H1D0000 } )
CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0001 } )
CreateDecimal( New Integer( ) _
{ &HF0000, &HF0000, &HF0000, &HF0000 } )
End Sub
End Module
' This example of the Decimal( Integer( ) ) constructor
輸出:
Constructor Value or Exception ----------- ------------------ Decimal( { &H0, &H0, &H0, &H0 } ) 0 Decimal( { &H0, &H0, &H0 } ) ArgumentException Decimal( { &H0, &H0, &H0, &H0, &H0 } ) ArgumentException Decimal( { &H3B9ACA00, &H0, &H0, &H0 } ) 1000000000 Decimal( { &H0, &H3B9ACA00, &H0, &H0 } ) 4294967296000000000 Decimal( { &H0, &H0, &H3B9ACA00, &H0 } ) 18446744073709551616000000000 Decimal( { &H0, &H0, &H0, &H3B9ACA00 } ) ArgumentException Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H0 } ) 79228162514264337593543950335 Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H80000000 } ) -79228162514264337593543950335 Decimal( { &HFFFFFFFF, &H0, &H0, &H100000 } ) 0.0000004294967295 Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0000 } ) 0.0000000000000000004294967295 Decimal( { &HFFFFFFFF, &H0, &H0, &H1D0000 } ) ArgumentException Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0001 } ) ArgumentException Decimal( { &HF0000, &HF0000, &HF0000, &HF0000 } ) 18133887298.441562272235520
示例4: DecimalCtorLDemo
' Example of the Decimal( Long ) constructor.
Module DecimalCtorLDemo
' Create a Decimal object and display its value.
Sub CreateDecimal( value As Long, valToStr As String )
Dim decimalNum As New Decimal( value )
' Format the constructor for display.
Dim ctor As String = _
String.Format( "Decimal( {0} )", valToStr )
' Display the constructor and its value.
Console.WriteLine( "{0,-30}{1,22}", ctor, decimalNum )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( Long ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-30}{1,22}", "Constructor", "Value" )
Console.WriteLine( "{0,-30}{1,22}", "-----------", "-----" )
' Construct Decimal objects from Long values.
CreateDecimal( Long.MinValue, "Long.MinValue" )
CreateDecimal( Long.MaxValue, "Long.MaxValue" )
CreateDecimal( 0L, "0" )
CreateDecimal( 999999999999999999, "999999999999999999" )
CreateDecimal( &H2000000000000000, "&H2000000000000000" )
CreateDecimal( &HE000000000000000, "&HE000000000000000" )
End Sub
End Module
' This example of the Decimal( Long ) constructor
輸出:
Constructor Value ----------- ----- Decimal( Long.MinValue ) -9223372036854775808 Decimal( Long.MaxValue ) 9223372036854775807 Decimal( 0 ) 0 Decimal( 999999999999999999 ) 999999999999999999 Decimal( &H2000000000000000 ) 2305843009213693952 Decimal( &HE000000000000000 ) -2305843009213693952
示例5: DecimalCtorSDemo
' Example of the Decimal( Single ) constructor.
Module DecimalCtorSDemo
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
' Create a Decimal object and display its value.
Sub CreateDecimal( value As Single, valToStr As String )
' Format and display the constructor.
Console.Write( "{0,-27}", _
String.Format( "Decimal( {0} )", valToStr ) )
' Construct the Decimal value.
Try
Dim decimalNum As New Decimal( value )
' Display the value if it was created successfully.
Console.WriteLine( "{0,31}", decimalNum )
' Display the exception type if an exception was thrown.
Catch ex As Exception
Console.WriteLine( "{0,31}", GetExceptionType( ex ) )
End Try
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( Single ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-27}{1,31}", "Constructor", "Value or Exception" )
Console.WriteLine( "{0,-27}{1,31}", "-----------", "------------------" )
' Construct Decimal objects from Single values.
CreateDecimal( 1.2345E+5, "1.2345E+5" )
CreateDecimal( 1.234567E+15, "1.234567E+15" )
CreateDecimal( 1.23456789E+25, "1.23456789E+25" )
CreateDecimal( 1.23456789E+35, "1.23456789E+35" )
CreateDecimal( 1.2345E-5, "1.2345E-5" )
CreateDecimal( 1.234567E-15, "1.234567E-15" )
CreateDecimal( 1.23456789E-25, "1.23456789E-25" )
CreateDecimal( 1.23456789E-35, "1.23456789E-35" )
CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" )
End Sub
End Module
' This example of the Decimal( Single ) constructor
輸出:
Constructor Value or Exception ----------- ------------------ Decimal( 1.2345E+5 ) 123450 Decimal( 1.234567E+15 ) 1234567000000000 Decimal( 1.23456789E+25 ) 12345680000000000000000000 Decimal( 1.23456789E+35 ) OverflowException Decimal( 1.2345E-5 ) 0.000012345 Decimal( 1.234567E-15 ) 0.000000000000001234567 Decimal( 1.23456789E-25 ) 0.0000000000000000000000001235 Decimal( 1.23456789E-35 ) 0 Decimal( 1.0 / 7.0 ) 0.1428571
示例6: DecimalCtorUIDemo
' Example of the Decimal( UInt32 ) constructor.
Module DecimalCtorUIDemo
' Create a Decimal object and display its value.
Sub CreateDecimal( value As UInt32, valToStr As String )
Dim decimalNum As New Decimal( value )
' Format the constructor for display.
Dim ctor As String = _
String.Format( "Decimal( {0} )", valToStr )
' Display the constructor and its value.
Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( UInt32 ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" )
Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" )
' Construct Decimal objects from UInt32 values.
' UInt32.MinValue and UInt32.MaxValue are not defined in VB.
CreateDecimal( Convert.ToUInt32( 0 ), """UInt32.MinValue""" )
CreateDecimal( Convert.ToUInt32( 4294967295 ), _
"""UInt32.MaxValue""" )
CreateDecimal( Convert.ToUInt32( Integer.MaxValue ), _
"Integer.MaxValue" )
CreateDecimal( Convert.ToUInt32( 999999999 ), "999999999" )
CreateDecimal( Convert.ToUInt32( &H40000000 ), "&H40000000" )
CreateDecimal( Convert.ToUInt32( &HC0000000L ), "&HC0000000" )
End Sub
End Module
' This example of the Decimal( UInt32 ) constructor
輸出:
Constructor Value ----------- ----- Decimal( "UInt32.MinValue" ) 0 Decimal( "UInt32.MaxValue" ) 4294967295 Decimal( Integer.MaxValue ) 2147483647 Decimal( 999999999 ) 999999999 Decimal( &H40000000 ) 1073741824 Decimal( &HC0000000 ) 3221225472
示例7: DecimalCtorULDemo
' Example of the Decimal( UInt64 ) constructor.
Module DecimalCtorULDemo
' Create a Decimal object and display its value.
Sub CreateDecimal( value As UInt64, valToStr As String )
Dim decimalNum As New Decimal( value )
' Format the constructor for display.
Dim ctor As String = _
String.Format( "Decimal( {0} )", valToStr )
' Display the constructor and its value.
Console.WriteLine( "{0,-33}{1,22}", ctor, decimalNum )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( UInt64 ) constructor " & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( "{0,-33}{1,22}", "Constructor", "Value" )
Console.WriteLine( "{0,-33}{1,22}", "-----------", "-----" )
' Construct Decimal objects from UInt64 values.
' UInt64.MinValue and UInt64.MaxValue are not defined in VB.
CreateDecimal( Convert.ToUInt64( 0 ), """UInt64.MinValue""" )
CreateDecimal( Convert.ToUInt64( 18446744073709551615D ), _
"""UInt64.MaxValue""" )
CreateDecimal( Convert.ToUInt64( Long.MaxValue ), _
"Long.MaxValue" )
CreateDecimal( Convert.ToUInt64( 999999999999999999 ), _
"999999999999999999" )
CreateDecimal( Convert.ToUInt64( &H2000000000000000 ), _
"&H2000000000000000" )
CreateDecimal( Convert.ToUInt64( 16140901064495857664.0 ), _
"16140901064495857664.0" )
End Sub
End Module
' This example of the Decimal( UInt64 ) constructor
輸出:
Constructor Value ----------- ----- Decimal( "UInt64.MinValue" ) 0 Decimal( "UInt64.MaxValue" ) 18446744073709551615 Decimal( Long.MaxValue ) 9223372036854775807 Decimal( 999999999999999999 ) 999999999999999999 Decimal( &H2000000000000000 ) 2305843009213693952 Decimal( 16140901064495857664.0 ) 16140901064495857664
示例8: DecimalCtorIIIBByDemo
' Example of the Decimal( Integer, Integer, Integer, Boolean, Byte )
' constructor.
Module DecimalCtorIIIBByDemo
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
' Create a Decimal object and display its value.
Sub CreateDecimal( low As Integer, mid As Integer, _
high As Integer, isNeg As Boolean, scale as Byte )
' Format the constructor for display.
Dim ctor As String = String.Format( _
"Decimal( {0}, {1}, {2}, {3}, {4} )", _
low, mid, high, isNeg, scale )
Dim valOrExc As String
' Construct the Decimal value.
Try
Dim decimalNum As New Decimal( _
low, mid, high, isNeg, scale )
' Format and save the Decimal value.
valOrExc = decimalNum.ToString( )
' Save the exception type if an exception was thrown.
Catch ex As Exception
valOrExc = GetExceptionType( ex )
End Try
' Display the constructor and Decimal value or exception.
Dim ctorLen = 76 - valOrExc.Length
If ctorLen > ctor.Length Then
' Display the data on one line if it will fit.
Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), _
valOrExc )
' Otherwise, display the data on two lines.
Else
Console.WriteLine( "{0}", ctor )
Console.WriteLine( "{0,76}", valOrExc )
End If
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the Decimal( Integer, Integer, " & _
"Integer, Boolean, Byte ) " & vbCrLf & "constructor " & _
"generates the following output." & vbCrLf )
Console.WriteLine( "{0,-38}{1,38}", "Constructor", _
"Value or Exception" )
Console.WriteLine( "{0,-38}{1,38}", "-----------", _
"------------------" )
' Construct Decimal objects from the component fields.
CreateDecimal( 0, 0, 0, False, 0 )
CreateDecimal( 0, 0, 0, False, 27 )
CreateDecimal( 0, 0, 0, True, 0 )
CreateDecimal( 1000000000, 0, 0, False, 0 )
CreateDecimal( 0, 1000000000, 0, False, 0 )
CreateDecimal( 0, 0, 1000000000, False, 0 )
CreateDecimal( 1000000000, 1000000000, 1000000000, False, 0 )
CreateDecimal( -1, -1, -1, False, 0 )
CreateDecimal( -1, -1, -1, True, 0 )
CreateDecimal( -1, -1, -1, False, 15 )
CreateDecimal( -1, -1, -1, False, 28 )
CreateDecimal( -1, -1, -1, False, 29 )
CreateDecimal( Integer.MaxValue, 0, 0, False, 18 )
CreateDecimal( Integer.MaxValue, 0, 0, False, 28 )
CreateDecimal( Integer.MaxValue, 0, 0, True, 28 )
End Sub
End Module
' This example of the Decimal( Integer, Integer, Integer, Boolean, Byte )
輸出:
Constructor Value or Exception ----------- ------------------ Decimal( 0, 0, 0, False, 0 ) 0 Decimal( 0, 0, 0, False, 27 ) 0 Decimal( 0, 0, 0, True, 0 ) 0 Decimal( 1000000000, 0, 0, False, 0 ) 1000000000 Decimal( 0, 1000000000, 0, False, 0 ) 4294967296000000000 Decimal( 0, 0, 1000000000, False, 0 ) 18446744073709551616000000000 Decimal( 1000000000, 1000000000, 1000000000, False, 0 ) 18446744078004518913000000000 Decimal( -1, -1, -1, False, 0 ) 79228162514264337593543950335 Decimal( -1, -1, -1, True, 0 ) -79228162514264337593543950335 Decimal( -1, -1, -1, False, 15 ) 79228162514264.337593543950335 Decimal( -1, -1, -1, False, 28 ) 7.9228162514264337593543950335 Decimal( -1, -1, -1, False, 29 ) ArgumentOutOfRangeException Decimal( 2147483647, 0, 0, False, 18 ) 0.000000002147483647 Decimal( 2147483647, 0, 0, False, 28 ) 0.0000000000000000002147483647 Decimal( 2147483647, 0, 0, True, 28 ) -0.0000000000000000002147483647
示例9: Example
Module Example
Public Sub Main()
Dim values() As Decimal = { 1234.96d, -1234.96d }
For Each value In values
Dim parts() = Decimal.GetBits(value)
Dim sign As Boolean = (parts(3) And &h80000000) <> 0
Dim scale As Byte = CByte((parts(3) >> 16) And &H7F)
Dim newValue As New Decimal(parts(0), parts(1), parts(2), sign, scale)
Console.WriteLine("{0} --> {1}", value, newValue)
Next
End Sub
End Module
輸出:
1234.96 --> 1234.96 -1234.96 --> -1234.96