Partager via


Convert.ToSingle Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function ToSingle ( _
    value As Object _
) As Single
public static float ToSingle(
    Object value
)

Parameters

Return Value

Type: System.Single
A single-precision floating point number equivalent to the value of value, or zero if value is nulla null reference (Nothing in Visual Basic).

Exceptions

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

Examples

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

Public Sub ConvertStringFloat(ByVal stringVal As String)
   Dim singleVal As Single = 0

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

   ' Single to string conversion will not overflow.
   stringVal = System.Convert.ToString(singleVal)
   outputBlock.Text &= String.Format("The single as a string is {0}.", _
                             stringVal) & vbCrLf
End Sub
public void ConvertStringFloat(string stringVal)
{
   float floatVal = 0;

   try
   {
      floatVal = System.Convert.ToSingle(stringVal);
      outputBlock.Text += String.Format(
         "The string as a float is {0}.", floatVal) + "\n";
   }
   catch (System.OverflowException)
   {
      outputBlock.Text += String.Format(
         "The conversion from string-to-float overflowed.") + "\n";
   }
   catch (System.FormatException)
   {
      outputBlock.Text += String.Format(
         "The string is not formatted as a float.") + "\n";
   }
   catch (System.ArgumentNullException)
   {
      outputBlock.Text += String.Format(
         "The string is null.") + "\n";
   }

   // Float to string conversion will not overflow.
   stringVal = System.Convert.ToString(floatVal);
   outputBlock.Text += String.Format(
      "The float as a string is {0}.", stringVal) + "\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.