Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal Narrowing Conversion (Single to Decimal)

Defines an explicit conversion of a single-precision floating-point number to a Decimal.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Narrowing Operator CType ( _
	value As Single _
) As Decimal

Parameters

value
Type: System.Single

The single-precision floating-point number to convert.

Return Value

Type: System.Decimal
The converted single-precision floating point number.

ExceptionCondition
OverflowException

value is less than Decimal.MinValue or greater than Decimal.MaxValue.

-or-

value is Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity.

The following example converts Single values to Decimal numbers by using the Single to Decimal conversion. This conversion requires the op_Explicit operator in Visual Basic.

' Example of the explicit conversion from Single to Decimal. 
Imports System
Imports Microsoft.VisualBasic

Module DecimalFromSingleDemo

    Const formatter As String = "{0,16:E7}{1,33}" 

    ' 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 

    ' Convert the Single argument; catch exceptions that are thrown. 
    Sub DecimalFromSingle( argument As Single )

        Dim decValue    As Object 

        ' Convert the Single argument to a Decimal value. 
        Try
            decValue = Decimal.op_Explicit( argument )
        Catch ex As Exception
            decValue = GetExceptionType( ex )
        End Try 

        ' Display the Decimal.
        Console.WriteLine( formatter, argument, decValue )
    End Sub  

    Sub Main( )

        Console.WriteLine( _
            "This example of the explicit conversion from Single " & _
            "to Decimal " & vbCrLf & "generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Single argument", _
            "Decimal value" )
        Console.WriteLine( formatter, "---------------", _
            "-------------" )

        ' Convert Single values and display the results.
        DecimalFromSingle( 1.2345E-30 )
        DecimalFromSingle( 1.2345E-26 )
        DecimalFromSingle( 1.23456E-22 )
        DecimalFromSingle( 1.23456E-12 )
        DecimalFromSingle( 1.234567 )
        DecimalFromSingle( 1.234567E+12 )
        DecimalFromSingle( 1.2345678E+28 ) 
        DecimalFromSingle( 1.2345678E+30 )
    End Sub  
End Module 

' This example of the explicit conversion from Single to Decimal 
' generates the following output. 
'  
'  Single argument                    Decimal value 
'  ---------------                    ------------- 
'   1.2345000E-030                                0 
'   1.2345000E-026   0.0000000000000000000000000123 
'   1.2345600E-022    0.000000000000000000000123456 
'   1.2345600E-012              0.00000000000123456 
'   1.2345671E+000                         1.234567 
'   1.2345670E+012                    1234567000000 
'   1.2345678E+028    12345680000000000000000000000 
'   1.2345678E+030                OverflowException

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft