Convert Decimal to Int64 (long) in C#

The Convert.ToInt64() method in C# converts a decimal value to a 64-bit signed integer (long). This conversion rounds the decimal to the nearest integer using banker's rounding (round to even) and truncates any fractional part.

Syntax

Following is the syntax for converting decimal to Int64 −

long result = Convert.ToInt64(decimalValue);

Parameters

  • decimalValue − The decimal number to be converted to Int64.

Return Value

Returns a 64-bit signed integer equivalent of the specified decimal value, rounded to the nearest integer.

Using Convert.ToInt64() for Basic Conversion

The following example demonstrates basic conversion from decimal to Int64 −

using System;

class Demo {
   static void Main() {
      decimal d = 190.66m;
      long res;
      res = Convert.ToInt64(d);
      Console.WriteLine("Converted Decimal '{0}' to Int64 value {1}", d, res);
   }
}

The output of the above code is −

Converted Decimal '190.66' to Int64 value 191

Understanding Rounding Behavior

The Convert.ToInt64() method uses banker's rounding, which rounds to the nearest even number when the fractional part is exactly 0.5 −

using System;

class RoundingDemo {
   static void Main() {
      decimal[] values = {2.5m, 3.5m, 4.5m, 5.5m, 190.66m, 310.23m};
      
      foreach (decimal d in values) {
         long result = Convert.ToInt64(d);
         Console.WriteLine("{0} converts to {1}", d, result);
      }
   }
}

The output of the above code is −

2.5 converts to 2
3.5 converts to 4
4.5 converts to 4
5.5 converts to 6
190.66 converts to 191
310.23 converts to 310

Decimal to Int64 Conversion Decimal 190.66m Int64 (long) 191 Convert.ToInt64() Banker's Rounding Applied Fractional part (.66) rounded up Result: 191

Handling Large Decimal Values

When converting large decimal values, ensure they fit within the Int64 range (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) −

using System;

class LargeValueDemo {
   static void Main() {
      try {
         decimal largeDecimal = 9223372036854775807m;
         decimal tooLarge = 9223372036854775808m;
         
         long result1 = Convert.ToInt64(largeDecimal);
         Console.WriteLine("Converted {0} to {1}", largeDecimal, result1);
         
         long result2 = Convert.ToInt64(tooLarge);
         Console.WriteLine("This won't execute due to overflow");
      }
      catch (OverflowException ex) {
         Console.WriteLine("OverflowException: Value too large for Int64");
      }
   }
}

The output of the above code is −

Converted 9223372036854775807 to 9223372036854775807
OverflowException: Value too large for Int64

Comparison of Conversion Methods

Method Behavior Exception on Overflow
Convert.ToInt64() Banker's rounding Yes
(long) cast Truncation (no rounding) Yes
decimal.ToInt64() Banker's rounding Yes

Conclusion

The Convert.ToInt64() method provides a reliable way to convert decimal values to Int64 using banker's rounding. It handles fractional parts by rounding to the nearest integer and throws an OverflowException if the decimal value exceeds the Int64 range.

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

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements