This documentation is archived and is not being maintained.

Convert.ToSingle Method (String)

Converts the specified String representation of a number to an equivalent single-precision floating point number.

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

public static float ToSingle (
	string value
)
public static float ToSingle (
	String value
)
public static function ToSingle (
	value : String
) : float
Not applicable.

Parameters

value

A String containing a number to convert.

Return Value

A single-precision floating point number equivalent to the value of value. -or- Zero if value is a null reference (Nothing in Visual Basic).

Exception typeCondition

FormatException

value is not a number in a valid format.

OverflowException

value represents a number less than MinValue or greater than MaxValue.

The return value is the result of invoking the Single.Parse method on value.

The following code sample illustrates the conversion of a String to Single, using ToSingle :

public void ConvertStringFloat(string stringVal) {
    float floatVal = 0;
    
    try {
        floatVal = System.Convert.ToSingle(stringVal);
        System.Console.WriteLine(
            "The string as a float is {0}.", floatVal);
    } 
    catch (System.OverflowException){
        System.Console.WriteLine(
            "The conversion from string-to-float overflowed.");
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string is not formatted as a float.");
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine(
            "The string is null.");
    }

    // Float to string conversion will not overflow.
    stringVal = System.Convert.ToString(floatVal);
    System.Console.WriteLine(
        "The float as a string is {0}.", stringVal);
}

public void ConvertStringFloat(String stringVal)
{
    float floatVal = 0;

    try {        
        floatVal = System.Convert.ToSingle(stringVal);
        System.Console.WriteLine("The string as a float is {0}.", 
            System.Convert.ToString(floatVal));
    }
    catch (System.OverflowException exp) {        
        System.Console.WriteLine(
            "The conversion from string-to-float overflowed.");
    }
    catch (System.FormatException exp) {                    
        System.Console.WriteLine("The string is not formatted as a float.");
    }
    catch (System.ArgumentNullException exp){        
        System.Console.WriteLine("The string is null.");
    }

    // Float to string conversion will not overflow.
    stringVal = System.Convert.ToString(floatVal);
    System.Console.WriteLine("The float as a string is {0}.", stringVal);
} //ConvertStringFloat

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: