Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Convert.ToSingle Method (Object)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static float ToSingle(
	Object value
)

Parameters

value
Type: System.Object

An object that implements the IConvertible interface, or null.

Return Value

Type: System.Single
A single-precision floating-point number that is equivalent to value, or zero if value is null.
ExceptionCondition
FormatException

value is not in an appropriate format.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

OverflowException

value represents a number that is less than Single.MinValue or greater than Single.MaxValue.

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

The following example attempts to convert each element in an object array to a Single value.

object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02",
                    1.67e03, "A100", "1,033.67", DateTime.Now,
                    Decimal.MaxValue };   
float result;

foreach (object value in values)
{
   try {
      result = Convert.ToSingle(value);
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", 
                        value.GetType().Name, value, 
                        result.GetType().Name, result);
   }
   catch (FormatException) {
      Console.WriteLine("The {0} value {1} is not recognized as a valid Single value.",
                        value.GetType().Name, value);
   }                     
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Single type.",
                        value.GetType().Name, value);
   }
   catch (InvalidCastException) {
      Console.WriteLine("Conversion of the {0} value {1} to a Single is not supported.",
                        value.GetType().Name, value);
   }                     
}
// The example displays the following output: 
//    Converted the Boolean value 'True' to the Single value 1. 
//    Conversion of the Char value a to a Single is not supported. 
//    Converted the Int32 value '123' to the Single value 123. 
//    Converted the Double value '1.764E+32' to the Single value 1.764E+32. 
//    Converted the String value '9.78' to the Single value 9.78. 
//    Converted the String value '1e-02' to the Single value 0.01. 
//    Converted the Double value '1670' to the Single value 1670. 
//    The String value A100 is not recognized as a valid Single value. 
//    Converted the String value '1,033.67' to the Single value 1033.67. 
//    Conversion of the DateTime value 11/7/2008 08:02:35 AM to a Single is not supported. 
//    Converted the Decimal value '79228162514264337593543950335' to the Single value 7.922816E+28.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.