Explicit Conversion (Single to Decimal)

Decimal Explicit Conversion (Single to Decimal)

.NET Framework (current version)

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

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

Syntax

```public:
static explicit operator Decimal (
float value
)
```

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.

Exceptions

Exception Condition
OverflowException

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

-or-

Examples

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 float to Decimal.
using namespace System;
#define formatter "{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 = (Decimal)argument;
}
catch ( Exception^ ex )
{
decValue = GetExceptionType( ex );
}

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

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

Version Information

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1