Convert.ToSingle Method
Converts a specified value to a single-precision floating point number.
Overload List
Converts the value of the specified Boolean value to the equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Boolean) As Single
[C#] public static float ToSingle(bool);
[C++] public: static float ToSingle(bool);
[JScript] public static function ToSingle(Boolean) : float;
Converts the value of the specified 8-bit unsigned integer to the equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Byte) As Single
[C#] public static float ToSingle(byte);
[C++] public: static float ToSingle(unsigned char);
[JScript] public static function ToSingle(Byte) : float;
Calling this method always throws InvalidCastException.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Char) As Single
[C#] public static float ToSingle(char);
[C++] public: static float ToSingle(__wchar_t);
[JScript] public static function ToSingle(Char) : float;
Calling this method always throws InvalidCastException.
[Visual Basic] Overloads Public Shared Function ToSingle(DateTime) As Single
[C#] public static float ToSingle(DateTime);
[C++] public: static float ToSingle(DateTime);
[JScript] public static function ToSingle(DateTime) : float;
Converts the value of the specified Decimal number to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Decimal) As Single
[C#] public static float ToSingle(decimal);
[C++] public: static float ToSingle(Decimal);
[JScript] public static function ToSingle(Decimal) : float;
Converts the value of the specified double-precision floating point number to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Double) As Single
[C#] public static float ToSingle(double);
[C++] public: static float ToSingle(double);
[JScript] public static function ToSingle(double) : float;
Converts the value of the specified 16-bit signed integer to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Short) As Single
[C#] public static float ToSingle(short);
[C++] public: static float ToSingle(short);
[JScript] public static function ToSingle(Int16) : float;
Converts the value of the specified 32-bit signed integer to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Integer) As Single
[C#] public static float ToSingle(int);
[C++] public: static float ToSingle(int);
[JScript] public static function ToSingle(int) : float;
Converts the value of the specified 64-bit signed integer to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Long) As Single
[C#] public static float ToSingle(long);
[C++] public: static float ToSingle(__int64);
[JScript] public static function ToSingle(long) : float;
Converts the value of the specified Object to a single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Object) As Single
[C#] public static float ToSingle(object);
[C++] public: static float ToSingle(Object*);
[JScript] public static function ToSingle(Object) : float;
Converts the value of the specified 8-bit signed integer to the equivalent single-precision floating point number. This method is not CLS-compliant.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(SByte) As Single
[C#] public static float ToSingle(sbyte);
[C++] public: static float ToSingle(char);
[JScript] public static function ToSingle(SByte) : float;
Returns the specified single-precision floating point number; no actual conversion is performed.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(Single) As Single
[C#] public static float ToSingle(float);
[C++] public: static float ToSingle(float);
[JScript] public static function ToSingle(float) : float;
Converts the specified String representation of a number to an equivalent single-precision floating point number.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(String) As Single
[C#] public static float ToSingle(string);
[C++] public: static float ToSingle(String*);
[JScript] public static function ToSingle(String) : float;
Converts the value of the specified 16-bit unsigned integer to the equivalent single-precision floating point number. This method is not CLS-compliant.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(UInt16) As Single
[C#] public static float ToSingle(ushort);
[C++] public: static float ToSingle(unsigned short);
[JScript] public static function ToSingle(UInt16) : float;
Converts the value of the specified 32-bit unsigned integer to an equivalent single-precision floating point number. This method is not CLS-compliant.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(UInt32) As Single
[C#] public static float ToSingle(uint);
[C++] public: static float ToSingle(unsigned int);
[JScript] public static function ToSingle(UInt32) : float;
Converts the value of the specified 64-bit unsigned integer to an equivalent single-precision floating point number. This method is not CLS-compliant.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(UInt64) As Single
[C#] public static float ToSingle(ulong);
[C++] public: static float ToSingle(unsigned __int64);
[JScript] public static function ToSingle(UInt64) : float;
Converts the value of the specified Object to an single-precision floating point number using the specified culture-specific formatting information.
[Visual Basic] Overloads Public Shared Function ToSingle(Object, IFormatProvider) As Single
[C#] public static float ToSingle(object, IFormatProvider);
[C++] public: static float ToSingle(Object*, IFormatProvider*);
[JScript] public static function ToSingle(Object, IFormatProvider) : float;
Converts the specified String representation of a number to an equivalent single-precision floating point number using the specified culture-specific formatting information.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ToSingle(String, IFormatProvider) As Single
[C#] public static float ToSingle(string, IFormatProvider);
[C++] public: static float ToSingle(String*, IFormatProvider*);
[JScript] public static function ToSingle(String, IFormatProvider) : float;
Example
[Visual Basic, C#, C++] The following code example converts String representations of Single values with the ToSingle method, using an IFormatProvider object.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of ToSingle. For other examples that might be available, see the individual overload topics.
[Visual Basic] ' Example of the Convert.ToSingle( String ) and ' Convert.ToSingle( String, IFormatProvider ) methods. Imports System Imports System.Globalization Imports Microsoft.VisualBasic Module ToSingleProviderDemo Dim formatter As String = "{0,-22}{1,-20}{2}" ' Get the exception type name; remove the namespace prefix. Function GetExceptionType( ex As Exception ) As String Dim exceptionType As String = ex.GetType( ).ToString( ) GetExceptionType = exceptionType.Substring( _ exceptionType.LastIndexOf( "."c ) + 1 ) End Function Sub ConvertToSingle( numericStr As String, _ provider As IFormatProvider ) Dim defaultValue As Object Dim providerValue As Object ' Convert numericStr to Single without a format provider. Try defaultValue = Convert.ToSingle( numericStr ) Catch ex As Exception defaultValue = GetExceptionType( ex ) End Try ' Convert numericStr to Single with a format provider. Try providerValue = Convert.ToSingle( numericStr, provider ) Catch ex As Exception providerValue = GetExceptionType( ex ) End Try Console.WriteLine( formatter, numericStr, _ defaultValue, providerValue ) End Sub Sub Main( ) ' Create a NumberFormatInfo object and set several of its ' properties that apply to numbers. Dim provider As NumberFormatInfo = new NumberFormatInfo( ) provider.NumberDecimalSeparator = "," provider.NumberGroupSeparator = "." provider.NumberGroupSizes = New Integer( ) { 3 } Console.WriteLine( "This example of" & vbCrLf & _ " Convert.ToSingle( String ) and " & vbCrLf & _ " Convert.ToSingle( String, IFormatProvider ) " & _ vbCrLf & "generates the " & _ "following output when run in the [{0}] culture.", _ CultureInfo.CurrentCulture.Name ) Console.WriteLine( vbCrLf & _ "Several strings are converted to Single values, " & _ "using " & vbCrLf & "default formatting " & _ "and a NumberFormatInfo object." & vbCrLf ) Console.WriteLine( formatter, "String to convert", _ "Default/exception", "Provider/exception" ) Console.WriteLine( formatter, "-----------------", _ "-----------------", "------------------" ) ' Convert strings, with and without an IFormatProvider. ConvertToSingle( "1234567", provider ) ConvertToSingle( "1234.567", provider ) ConvertToSingle( "1234,567", provider ) ConvertToSingle( "12,345.67", provider ) ConvertToSingle( "12.345,67", provider ) ConvertToSingle( "1,234,567.89", provider ) ConvertToSingle( "1.234.567,89", provider ) End Sub End Module ' This example of ' Convert.ToSingle( String ) and ' Convert.ToSingle( String, IFormatProvider ) ' generates the following output when run in the [en-US] culture. ' ' Several strings are converted to Single values, using ' default formatting and a NumberFormatInfo object. ' ' String to convert Default/exception Provider/exception ' ----------------- ----------------- ------------------ ' 1234567 1234567 1234567 ' 1234.567 1234.567 1234567 ' 1234,567 1234567 1234.567 ' 12,345.67 12345.67 FormatException ' 12.345,67 FormatException 12345.67 ' 1,234,567.89 1234568 FormatException ' 1.234.567,89 FormatException 1234568 [C#] // Example of the Convert.ToSingle( String ) and // Convert.ToSingle( String, IFormatProvider ) methods. using System; using System.Globalization; class ToSingleProviderDemo { static string formatter = "{0,-22}{1,-20}{2}"; // Get the exception type name; remove the namespace prefix. static string GetExceptionType( Exception ex ) { string exceptionType = ex.GetType( ).ToString( ); return exceptionType.Substring( exceptionType.LastIndexOf( '.' ) + 1 ); } static void ConvertToSingle( string numericStr, IFormatProvider provider ) { object defaultValue; object providerValue; // Convert numericStr to float without a format provider. try { defaultValue = Convert.ToSingle( numericStr ); } catch( Exception ex ) { defaultValue = GetExceptionType( ex ); } // Convert numericStr to float with a format provider. try { providerValue = Convert.ToSingle( numericStr, provider ); } catch( Exception ex ) { providerValue = GetExceptionType( ex ); } Console.WriteLine( formatter, numericStr, defaultValue, providerValue ); } static void Main( ) { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. NumberFormatInfo provider = new NumberFormatInfo( ); provider.NumberDecimalSeparator = ","; provider.NumberGroupSeparator = "."; provider.NumberGroupSizes = new int[ ] { 3 }; Console.WriteLine( "This example of\n Convert.ToSingle( String ) and \n" + " Convert.ToSingle( String, IFormatProvider ) \n" + "generates the following output when run in the " + "[{0}] culture.", CultureInfo.CurrentCulture.Name ); Console.WriteLine( "\nSeveral " + "strings are converted to float values, using \n" + "default formatting and a NumberFormatInfo object.\n"); Console.WriteLine( formatter, "String to convert", "Default/exception", "Provider/exception" ); Console.WriteLine( formatter, "-----------------", "-----------------", "------------------" ); // Convert strings, with and without an IFormatProvider. ConvertToSingle( "1234567", provider ); ConvertToSingle( "1234.567", provider ); ConvertToSingle( "1234,567", provider ); ConvertToSingle( "12,345.67", provider ); ConvertToSingle( "12.345,67", provider ); ConvertToSingle( "1,234,567.89", provider ); ConvertToSingle( "1.234.567,89", provider ); } } /* This example of Convert.ToSingle( String ) and Convert.ToSingle( String, IFormatProvider ) generates the following output when run in the [en-US] culture. Several strings are converted to float values, using default formatting and a NumberFormatInfo object. String to convert Default/exception Provider/exception ----------------- ----------------- ------------------ 1234567 1234567 1234567 1234.567 1234.567 1234567 1234,567 1234567 1234.567 12,345.67 12345.67 FormatException 12.345,67 FormatException 12345.67 1,234,567.89 1234568 FormatException 1.234.567,89 FormatException 1234568 */ [C++] // Example of the Convert::ToSingle( String ) and // Convert::ToSingle( String, IFormatProvider ) methods. #using <mscorlib.dll> using namespace System; using namespace System::Globalization; const __wchar_t* formatter = L"{0,-22}{1,-20}{2}"; // Get the exception type name; remove the namespace prefix. String* GetExceptionType( Exception* ex ) { String* exceptionType = ex->GetType( )->ToString( ); return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 ); } void ConvertToSingle( String* numericStr, IFormatProvider* provider ) { Object* defaultValue; Object* providerValue; // Convert numericStr to float without a format provider. try { defaultValue = __box( Convert::ToSingle( numericStr ) ); } catch( Exception* ex ) { defaultValue = GetExceptionType( ex ); } // Convert numericStr to float with a format provider. try { providerValue = __box( Convert::ToSingle( numericStr, provider ) ); } catch( Exception* ex ) { providerValue = GetExceptionType( ex ); } Console::WriteLine( new String( formatter ), numericStr, defaultValue, providerValue ); } void main( ) { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. NumberFormatInfo* provider = new NumberFormatInfo( ); provider->NumberDecimalSeparator = S","; provider->NumberGroupSeparator = S"."; Int32 sizes __gc [] = { 3 }; provider->NumberGroupSizes = sizes; Console::WriteLine( S"This example of\n Convert::ToSingle( String* ) and \n" S" Convert::ToSingle( String*, IFormatProvider* ) \n" S"generates the following output when run in the " S"[{0}] culture.", CultureInfo::CurrentCulture->Name ); Console::WriteLine( S"\nSeveral " S"strings are converted to float values, using \n" S"default formatting and a NumberFormatInfo object.\n"); Console::WriteLine( new String( formatter ), S"String to convert", S"Default/exception", S"Provider/exception" ); Console::WriteLine( new String( formatter ), S"-----------------", S"-----------------", S"------------------" ); // Convert strings, with and without an IFormatProvider. ConvertToSingle( S"1234567", provider ); ConvertToSingle( S"1234.567", provider ); ConvertToSingle( S"1234,567", provider ); ConvertToSingle( S"12,345.67", provider ); ConvertToSingle( S"12.345,67", provider ); ConvertToSingle( S"1,234,567.89", provider ); ConvertToSingle( S"1.234.567,89", provider ); } /* This example of Convert::ToSingle( String* ) and Convert::ToSingle( String*, IFormatProvider* ) generates the following output when run in the [en-US] culture. Several strings are converted to float values, using default formatting and a NumberFormatInfo object. String to convert Default/exception Provider/exception ----------------- ----------------- ------------------ 1234567 1234567 1234567 1234.567 1234.567 1234567 1234,567 1234567 1234.567 12,345.67 12345.67 FormatException 12.345,67 FormatException 12345.67 1,234,567.89 1234568 FormatException 1.234.567,89 FormatException 1234568 */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.