This documentation is archived and is not being maintained.

Convert.ToSingle Method (Double)

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

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

public static float ToSingle (
	double value
)
public static float ToSingle (
	double value
)
public static function ToSingle (
	value : double
) : float

Parameters

value

A double-precision floating point number.

Return Value

A single-precision floating point number equivalent to the value of value. value is rounded using rounding to nearest. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36.

The following code sample illustrates the conversion of a Double to Single, using ToSingle.

public void CovertDoubleFloat(double doubleVal) {    
    float floatVal = 0;

    // Double to float conversion can overflow.
    try {
        floatVal = System.Convert.ToSingle(doubleVal);
        System.Console.WriteLine("{0} as a float is {1}",
            doubleVal, floatVal);
    } 
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in double-to-float conversion.");
    }

    // Conversion from float to double cannot overflow.
    doubleVal = System.Convert.ToDouble(floatVal);
    System.Console.WriteLine("{0} as a double is: {1}",
        floatVal, doubleVal);
}

public void CovertDoubleFloat(double doubleVal)
{
    float floatVal = 0;

    // Double to float conversion can overflow.
    try {        
        floatVal = System.Convert.ToSingle(doubleVal);
        System.Console.WriteLine("{0} as a float is {1}", 
            System.Convert.ToString(doubleVal), 
            System.Convert.ToString(floatVal));
    }
    catch (System.OverflowException exp) {        
        System.Console.WriteLine("Overflow in double-to-float conversion.");
    }

    // Conversion from float to double cannot overflow.
    doubleVal = System.Convert.ToDouble(floatVal);
    System.Console.WriteLine("{0} as a double is: {1}", 
        System.Convert.ToString(floatVal), 
        System.Convert.ToString(doubleVal));
} //CovertDoubleFloat

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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
Show: