Export (0) Print
Expand All

Decimal to Single Conversion

.NET Framework 1.1

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

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

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own. You can use the ToSingle method instead of the Decimal to Single conversion.

[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 Decimal to convert.

Parameters [C#]

value
A Decimal to convert.

Return Value

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

Remarks

This operation can produce round-off errors because a single-precision floating-point number has fewer significant digits than a Decimal.

Example

[C#, C++] The following code example converts Decimal numbers to Single values using the explicit Decimal to Single conversion.

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

class DecimalToSgl_DblDemo
{
    static string formatter = "{0,30}{1,17}{2,23}";

    // Convert the decimal argument; no exceptions are thrown.
    public static void DecimalToSgl_Dbl( decimal argument )
    {
        object SingleValue;
        object DoubleValue;

        // Convert the argument to a single value.
        SingleValue = (float)argument;

        // Convert the argument to a double value.
        DoubleValue = (double)argument;

        Console.WriteLine( formatter, argument, 
            SingleValue, DoubleValue );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversions from decimal " +
            "to float \nand decimal to double generates the " +
            "following output. It displays \nseveral converted " +
            "decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "float", "double" );
        Console.WriteLine( formatter, "----------------", 
            "-----", "------" );

        // Convert decimal values and display the results.
        DecimalToSgl_Dbl( 0.0000000000000000000000000001M );
        DecimalToSgl_Dbl( 0.0000000000123456789123456789M );
        DecimalToSgl_Dbl( 123M );
        DecimalToSgl_Dbl( new decimal( 123000000, 0, 0, false, 6 ) );
        DecimalToSgl_Dbl( 123456789.123456789M );
        DecimalToSgl_Dbl( 123456789123456789123456789M );
        DecimalToSgl_Dbl( decimal.MinValue );
        DecimalToSgl_Dbl( decimal.MaxValue );
    }
}

/*
This example of the explicit conversions from decimal to float
and decimal to double generates the following output. It displays
several converted decimal values.

              decimal argument            float                 double
              ----------------            -----                 ------
0.0000000000000000000000000001            1E-28                  1E-28
0.0000000000123456789123456789     1.234568E-11   1.23456789123457E-11
                           123              123                    123
                    123.000000              123                    123
           123456789.123456789     1.234568E+08       123456789.123457
   123456789123456789123456789     1.234568E+26   1.23456789123457E+26
-79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
 79228162514264337593543950335     7.922816E+28   7.92281625142643E+28
*/

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

static __wchar_t* formatter = L"{0,30}{1,17}{2,23}";

// Convert the Decimal argument; no exceptions are thrown.
void DecimalToSgl_Dbl( Decimal argument )
{
    Object* SingleValue;
    Object* DoubleValue;

    // Convert the argument to a float value.
    SingleValue = __box( (float)argument );

    // Convert the argument to a double value.
    DoubleValue = __box( (double)argument );

    Console::WriteLine( formatter, __box( argument ), 
        SingleValue, DoubleValue );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversions from Decimal to " 
        S"float \nand Decimal to double generates the " 
        S"following output. It \ndisplays several converted Decimal " 
        S"values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"float", S"double" );
    Console::WriteLine( formatter, S"----------------", 
        S"-----", S"------" );

    // Convert Decimal values and display the results.
    DecimalToSgl_Dbl( Decimal::Parse( "0.0000000000000000000000000001" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "0.0000000000123456789123456789" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123" ) );
    DecimalToSgl_Dbl( Decimal( 123000000, 0, 0, false, 6 ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123456789.123456789" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123456789123456789123456789" ) );
    DecimalToSgl_Dbl( Decimal::MinValue );
    DecimalToSgl_Dbl( Decimal::MaxValue );
}

/*
This example of the explicit conversions from Decimal to float
and Decimal to double generates the following output. It
displays several converted Decimal values.

              Decimal argument            float                 double
              ----------------            -----                 ------
0.0000000000000000000000000001            1E-28                  1E-28
0.0000000000123456789123456789     1.234568E-11   1.23456789123457E-11
                           123              123                    123
                    123.000000              123                    123
           123456789.123456789     1.234568E+08       123456789.123457
   123456789123456789123456789     1.234568E+26   1.23456789123457E+26
-79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
 79228162514264337593543950335     7.922816E+28   7.92281625142643E+28
*/

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter 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

See Also

Decimal Structure | Decimal Members | System Namespace | Single

Show:
© 2014 Microsoft