作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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
作者:VB.NET开发
项目:Syste
' 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 )
作者:VB.NET开发
项目:Syste
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