Export (0) Print
Expand All

Convert.ToSingle Method (Object)

.NET Framework 1.1

Converts the value of the specified Object to a single-precision floating point number.

[Visual Basic]
Overloads Public Shared Function ToSingle( _
   ByVal value As Object _
) As Single
[C#]
public static float ToSingle(
 object value
);
[C++]
public: static float ToSingle(
 Object* value
);
[JScript]
public static function ToSingle(
   value : Object
) : float;

Parameters

value
An Object that implements the IConvertible interface or a null reference (Nothing in Visual Basic).

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

Exceptions

Exception Type Condition
InvalidCastException value does not implement IConvertible.

Remarks

The return value is the result of invoking the IConvertible.ToSingle method of the underlying type of value.

Example

[Visual Basic, C#, C++] The following code sample illustrates the conversion of a String to Single, using ToSingle:

[Visual Basic] 
Public Sub ConvertStringFloat(ByVal stringVal As String)
    Dim singleVal As Single = 0

    Try
        singleVal = System.Convert.ToSingle(singleVal)
        System.Console.WriteLine("The string as a single is {0}.", _
                                  singleVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in string-to-single conversion.")
    Catch exception As System.FormatException
        System.Console.WriteLine( _
            "The string is not formatted as a Single.")
    Catch exception As System.ArgumentException
        System.Console.WriteLine("The string is null.")
    End Try

    ' Single to string conversion will not overflow.
    stringVal = System.Convert.ToString(singleVal)
    System.Console.WriteLine("The single as a string is {0}.", _
                              stringVal)
End Sub

[C#] 
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);
}

[C++] 

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

      try {
         floatVal = System::Convert::ToSingle(stringVal);
         System::Console::WriteLine(S"The String* as a float is {0}.", __box(floatVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"The conversion from String*-to-float overflowed.");
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* is not formatted as a float.");
      } catch (System::ArgumentNullException*) {
         System::Console::WriteLine(S"The String* is 0.");
      }

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

[JScript] No example is available for JScript. To view a Visual Basic, 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

See Also

Convert Class | Convert Members | System Namespace | Convert.ToSingle Overload List

Show:
© 2014 Microsoft