ToInt64 Method (Single)

Convert.ToInt64 Method (Single)

.NET Framework 1.1

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

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

Parameters

value
A single-precision floating point number.

Return Value

value rounded to the nearest 64-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

Exceptions

Exception Type Condition
OverflowException value is greater than Int64.MaxValue or less than Int64.MinValue.

Example

[Visual Basic, C#, C++] The following code sample illustrates the conversion of a Single value to an Int64, using ToInt64.

[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.ToInt64 Overload List | Single

Show:
© 2016 Microsoft