Export (0) Print
Expand All

Convert.ToSingle Method (Int64)

.NET Framework 1.1

Converts the value of the specified 64-bit signed integer to an equivalent single-precision floating point number.

[Visual Basic]
Overloads Public Shared Function ToSingle( _
   ByVal value As Long _
) As Single
[C#]
public static float ToSingle(
 long value
);
[C++]
public: static float ToSingle(
 __int64 value
);
[JScript]
public static function ToSingle(
   value : long
) : float;

Parameters

value
A 64-bit signed integer.

Return Value

A single-precision floating point number equivalent to the value of value.

Example

The following code sample illustrates the conversion of an Int64 to Single, using ToSingle. Note that large Int64 values can cause OverflowException to be thrown.

[Visual Basic] 
Public Sub ConvertLongFloat(ByVal longVal As Long)

    Dim floatVal As Single

    ' A conversion from Long to float cannot overflow.
    floatVal = System.Convert.ToSingle(longVal)
    System.Console.WriteLine("{0} as a float is {1}", _
                              longVal, floatVal)

    ' A conversion from float to long can overflow.
    Try
        longVal = System.Convert.ToInt64(floatVal)
        System.Console.WriteLine("{0} as a Long is {1}", _
                                  floatVal, longVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in float-to-long conversion.")
    End Try
End Sub

[C#] 
public void ConvertLongFloat(long longVal) {

    float    floatVal;
    
    // A conversion from Long to float cannot overflow.
    floatVal = System.Convert.ToSingle(longVal);
    System.Console.WriteLine("{0} as a float is {1}", 
            longVal, floatVal);
    
    // A conversion from float to long can overflow.
    try {
        longVal = System.Convert.ToInt64(floatVal);
        System.Console.WriteLine("{0} as a long is {1}", 
            floatVal, longVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in float-to-long conversion.");
    }
}

[C++] 

   void ConvertLongFloat(Int64 longVal)
   {
      float    floatVal;

      // A conversion from Long to float cannot overflow.
      floatVal = System::Convert::ToSingle(longVal);
      System::Console::WriteLine(S" {0} as a float is {1}", 
         __box(longVal), __box(floatVal));

      // A conversion from float to long can overflow.
      try {
         longVal = System::Convert::ToInt64(floatVal);
         System::Console::WriteLine(S" {0} as a long is {1}", 
            __box(floatVal), __box(longVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"Overflow in float-to-long conversion.");
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToSingle Overload List

Show:
© 2014 Microsoft