Export (0) Print
Expand All

Convert.ToString Method (Single)

Converts the value of the specified single-precision floating point number to its equivalent String representation.

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

public static string ToString (
	float value
)
public static String ToString (
	float value
)
public static function ToString (
	value : float
) : String

Parameters

value

A single-precision floating point number.

Return Value

The String equivalent of the value of value.

This implementation is identical to Single.ToString.

The following code sample illustrates the conversion of a Single to a String, using ToString.

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 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

Community Additions

ADD
Show:
© 2014 Microsoft