Convert.ToInt64 Method in C#

The Convert.ToInt64 method in C# converts a specified value to a 64-bit signed integer (long). This method can convert various data types including strings, floating-point numbers, and other numeric types to a long value.

Syntax

Following are the common syntax overloads for Convert.ToInt64 −

public static long ToInt64(object value);
public static long ToInt64(string value);
public static long ToInt64(double value);
public static long ToInt64(float value);
public static long ToInt64(decimal value);

Parameters

  • value − The value to convert to a 64-bit signed integer

Return Value

Returns a 64-bit signed integer (long) that is equivalent to the input value, or zero if the value is null.

Converting Double to Int64

When converting from double to long, the decimal portion is truncated (rounded toward zero) −

using System;

public class Demo {
   public static void Main() {
      double doubleNum = 193.834;
      long res;
      res = Convert.ToInt64(doubleNum);
      Console.WriteLine("Converted {0} to {1}", doubleNum, res);
      
      double negativeNum = -456.789;
      long negativeRes = Convert.ToInt64(negativeNum);
      Console.WriteLine("Converted {0} to {1}", negativeNum, negativeRes);
   }
}

The output of the above code is −

Converted 193.834 to 193
Converted -456.789 to -456

Converting String to Int64

The method can also convert string representations of numbers to long values −

using System;

public class Demo {
   public static void Main() {
      string numStr = "9876543210";
      long result = Convert.ToInt64(numStr);
      Console.WriteLine("String '{0}' converted to {1}", numStr, result);
      
      string hexStr = "FF";
      long hexResult = Convert.ToInt64(hexStr, 16);
      Console.WriteLine("Hex '{0}' converted to {1}", hexStr, hexResult);
   }
}

The output of the above code is −

String '9876543210' converted to 9876543210
Hex 'FF' converted to 255

Converting Different Data Types

Convert.ToInt64 works with various data types including bool, char, and other numeric types −

using System;

public class Demo {
   public static void Main() {
      bool boolVal = true;
      char charVal = 'A';
      decimal decimalVal = 12345.67m;
      float floatVal = 987.65f;
      
      Console.WriteLine("bool {0} to long: {1}", boolVal, Convert.ToInt64(boolVal));
      Console.WriteLine("char '{0}' to long: {1}", charVal, Convert.ToInt64(charVal));
      Console.WriteLine("decimal {0} to long: {1}", decimalVal, Convert.ToInt64(decimalVal));
      Console.WriteLine("float {0} to long: {1}", floatVal, Convert.ToInt64(floatVal));
   }
}

The output of the above code is −

bool True to long: 1
char 'A' to long: 65
decimal 12345.67 to long: 12345
float 987.65 to long: 987

Conclusion

The Convert.ToInt64 method provides a flexible way to convert various data types to 64-bit signed integers in C#. It handles type conversion safely and truncates decimal values when converting from floating-point numbers, making it useful for numeric data processing and type conversions.

Updated on: 2026-03-17T07:04:35+05:30

723 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements