Export (0) Print
Expand All

Decimal.ToSingle Method

Converts the value of the specified Decimal to the equivalent single-precision floating-point number.

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

public static float ToSingle (
	decimal d
)
public static float ToSingle (
	Decimal d
)
public static function ToSingle (
	d : decimal
) : float

Parameters

d

A Decimal value to convert.

Return Value

A single-precision floating-point number equivalent to the value of d.

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

The following code example converts Decimal numbers to Single values using the ToSingle method.

// Example of the decimal.ToSingle and decimal.ToDouble methods.
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 float value.
        SingleValue = decimal.ToSingle( argument );

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

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

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToSingle( decimal ) and \n" +
            "  decimal.ToDouble( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several 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
  decimal.ToSingle( decimal ) and
  decimal.ToDouble( decimal )
methods 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
*/

// Example of the decimal.ToSingle and decimal.ToDouble methods.
import System.* ;

class DecimalToSgl_DblDemo
{
    private static String formatter = "{0,30}{1,17}{2,23}";
      
    // Convert the decimal argument; no exceptions are thrown.
    public static void DecimalToSgl_Dbl(System.Decimal argument) 
    {
        Object singleValue;
        Object doubleValue;
      
        // Convert the argument to a float value.
        singleValue = (System.Single)System.Decimal.ToSingle(argument);
          
        // Convert the argument to a double value.
        doubleValue = (System.Double)System.Decimal.ToDouble(argument);
        Console.WriteLine(formatter, argument, singleValue, doubleValue);
    }
      
    public static void main(String[] args)
    {
        Console.WriteLine("This example of the \n" 
            + "  decimal.ToSingle( decimal ) and \n" 
            + "  decimal.ToDouble( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several converted decimal values.\n");
        
        Console.WriteLine(formatter, "decimal argument", "float", "double");
        Console.WriteLine(formatter, "----------------", "-----", "------");
            
        // Convert decimal values and display the results.
        DecimalToSgl_Dbl(new System.Decimal(0.0000000000000000000000000001));
        DecimalToSgl_Dbl(new System.Decimal(0.0000000000123456789123456789));
        DecimalToSgl_Dbl(new System.Decimal(123));
        DecimalToSgl_Dbl(new System.Decimal(123000000, 0, 0, false, (ubyte)6));
        DecimalToSgl_Dbl(new System.Decimal(123456789.123456789));
        DecimalToSgl_Dbl(new System.Decimal(123456789123456789123456789F));
        DecimalToSgl_Dbl(System.Decimal.MinValue);
        DecimalToSgl_Dbl(System.Decimal.MaxValue);
    }
}

/*
This example of the
  decimal.ToSingle( decimal ) and
  decimal.ToDouble( decimal )
methods generates the following output. It
displays several converted decimal values.

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft