Export (0) Print
Expand All

Decimal Explicit Conversion (Decimal to Single)

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

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

static explicit operator float (
	Decimal value
)

Parameters

value
Type: System::Decimal

The value to convert.

Return Value

Type: System::Single
A single-precision floating-point number that represents the converted Decimal.

This operation can result in a loss of precision, because a single-precision floating-point number has fewer significant digits than a Decimal.

This operator supports the explicit conversion of a Decimal to a Single. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to a Single value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToSingle or the Convert::ToSingle(Decimal) method.

The following example converts Decimal numbers to Single values by using the explicit Decimal to Single conversion.

using namespace System;

void main()
{
   // Define an array of Decimal values. 
   array<Decimal>^ values = { Decimal::Parse("0.0000000000000000000000000001"), 
                              Decimal::Parse("0.0000000000123456789123456789"),
                              Decimal::Parse("123"), 
                              Decimal(123000000, 0, 0, false, 6),
                              Decimal::Parse("123456789.123456789"), 
                              Decimal::Parse("123456789123456789123456789"), 
                              Decimal::MinValue, Decimal::MaxValue };
   // Convert each value to a double. 
   for each (Decimal value in values) {
       float dblValue = (float) value;
       Console::WriteLine("{0} ({1}) --> {2} ({3})", value,
                          value.GetType()->Name, dblValue, 
                          dblValue.GetType()->Name);
   }
}
// The example displays the following output: 
//    0.0000000000000000000000000001 (Decimal) --> 1E-28 (Single) 
//    0.0000000000123456789123456789 (Decimal) --> 1.234568E-11 (Single) 
//    123 (Decimal) --> 123 (Single) 
//    123.000000 (Decimal) --> 123 (Single) 
//    123456789.123456789 (Decimal) --> 1.234568E+08 (Single) 
//    123456789123456789123456789 (Decimal) --> 1.234568E+26 (Single) 
//    -79228162514264337593543950335 (Decimal) --> -7.922816E+28 (Single) 
//    79228162514264337593543950335 (Decimal) --> 7.922816E+28 (Single)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft