Convert.ToSingle Method (Object)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the value of the specified Object to a single-precision floating point number.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Object
An Object that implements the IConvertible interface or null.
Return Value
Type: System.SingleA single-precision floating point number equivalent to the value of value, or zero if value is null.
| Exception | Condition |
|---|---|
| InvalidCastException | value does not implement IConvertible. |
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); 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"; }
Show: