ToSingle Method (String)

Convert.ToSingle Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

'Declaration
Public Shared Function ToSingle ( _
	value As String _
) As Single

Parameters

value
Type: System.String
A String containing a number to convert.

Return Value

Type: System.Single
A single-precision floating point number equivalent to the value of value.
-or-
Zero if value is Nothing.

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

If you prefer not to handle an exception if the conversion fails, you can call the Single.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft