This documentation is archived and is not being maintained.

# Single to Decimal Conversion

.NET Framework 1.1

Converts a single-precision floating-point number to a Decimal.

```[Visual Basic]
returnValue = Decimal.op_Explicit(value)
[C#]
public static explicit operator decimal(
float value
);
[C++]
public: static Decimal op_Explicit(
float value
);
[JScript]
returnValue = Decimal(value);```

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own.

[JScript] In JScript, you can use the conversion operators defined by a type, but you cannot define your own.

#### Arguments [Visual Basic, JScript]

value
A single-precision floating-point number.

#### Parameters [C#, C++]

value
A single-precision floating-point number.

#### Return Value

A Decimal that represents the converted single-precision floating point number.

#### Exceptions

Exception Type Condition
OverflowException value is less than MinValue or greater than MaxValue.

-or-

#### Example

[Visual Basic, C#, C++] The following code example converts Single values to Decimal numbers using the Single to Decimal conversion. This conversion requires the op_Explicit operator in Visual Basic.

```[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

[C#]
// Example of the explicit conversion from float to decimal.
using System;

class DecimalFromSingleDemo
{
const string formatter = "{0,16:E7}{1,33}";

// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType( Exception ex )
{
string exceptionType = ex.GetType( ).ToString( );
return exceptionType.Substring(
exceptionType.LastIndexOf( '.' ) + 1 );
}

// Convert the float argument; catch exceptions that are thrown.
public static void DecimalFromSingle( float argument )
{
object decValue;

// Convert the float argument to a decimal value.
try
{
decValue = (decimal)argument;
}
catch( Exception ex )
{
decValue = GetExceptionType( ex );
}

Console.WriteLine( formatter, argument, decValue );
}

public static void Main( )
{
Console.WriteLine(
"This example of the explicit conversion from float " +
"to decimal \ngenerates the following output.\n" );
Console.WriteLine( formatter, "float argument",
"decimal value" );
Console.WriteLine( formatter, "--------------",
"-------------" );

// Convert float values and display the results.
DecimalFromSingle( 1.2345E-30F );
DecimalFromSingle( 1.2345E-26F );
DecimalFromSingle( 1.23456E-22F );
DecimalFromSingle( 1.23456E-12F );
DecimalFromSingle( 1.234567F );
DecimalFromSingle( 1.234567E+12F );
DecimalFromSingle( 1.2345678E+28F );
DecimalFromSingle( 1.2345678E+30F );
}
}

/*
This example of the explicit conversion from float to decimal
generates the following output.

float 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
*/

[C++]
// Example of the explicit conversion from float to Decimal.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,16:E7}{1,33}";

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
String* exceptionType = ex->GetType( )->ToString( );
return exceptionType->Substring(
exceptionType->LastIndexOf( '.' ) + 1 );
}

// Convert the float argument; catch exceptions that are thrown.
void DecimalFromSingle( float argument )
{
Object* decValue;

// Convert the float argument to a Decimal value.
try
{
decValue = __box( (Decimal)argument );
}
catch( Exception* ex )
{
decValue = GetExceptionType( ex );
}

Console::WriteLine( formatter, __box( argument ), decValue );
}

void main( )
{
Console::WriteLine(
S"This example of the explicit conversion from float "
S"to Decimal \ngenerates the following output.\n" );
Console::WriteLine( formatter, S"float argument",
S"Decimal value" );
Console::WriteLine( formatter, S"--------------",
S"-------------" );

// Convert float values and display the results.
DecimalFromSingle( 1.2345E-30F );
DecimalFromSingle( 1.2345E-26F );
DecimalFromSingle( 1.23456E-22F );
DecimalFromSingle( 1.23456E-12F );
DecimalFromSingle( 1.234567F );
DecimalFromSingle( 1.234567E+12F );
DecimalFromSingle( 1.2345678E+28F );
DecimalFromSingle( 1.2345678E+30F );
}

/*
This example of the explicit conversion from float to Decimal
generates the following output.

float 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
*/
```

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.

#### Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard