Decimal.ToInt64() Method in C#

The Decimal.ToInt64() method in C# is used to convert the value of the specified Decimal to the equivalent 64-bit signed integer (long). This method truncates the decimal portion, returning only the whole number part.

Syntax

Following is the syntax −

public static long ToInt64(decimal val);

Parameters

  • val − The decimal number to convert to a 64-bit signed integer.

Return Value

This method returns a long value that represents the truncated decimal value. The fractional part is discarded, not rounded.

How It Works

The method performs truncation, meaning it simply removes the decimal portion without any rounding. For example, 123.89 becomes 123, and -45.67 becomes -45.

Decimal.ToInt64() Truncation Process 567.89m Decimal Input Truncate 567L Long Output

Using Basic Conversion

Example

using System;

public class Demo {
    public static void Main() {
        Decimal val1 = -567.7989m;
        Decimal val2 = 456.000m;
        
        Console.WriteLine("Decimal 1 = " + val1);
        Console.WriteLine("Decimal 2 = " + val2);
        
        long res1 = Decimal.ToInt64(val1);
        long res2 = Decimal.ToInt64(val2);
        
        Console.WriteLine("64-bit signed integer (value1) = " + res1);
        Console.WriteLine("64-bit signed integer (value2) = " + res2);
    }
}

The output of the above code is −

Decimal 1 = -567.7989
Decimal 2 = 456.000
64-bit signed integer (value1) = -567
64-bit signed integer (value2) = 456

Using Various Decimal Values

Example

using System;

public class Demo {
    public static void Main() {
        Decimal val1 = 1.00m;
        Decimal val2 = 34.678m;
        Decimal val3 = 0.999m;
        
        Console.WriteLine("Decimal 1 = " + val1);
        Console.WriteLine("Decimal 2 = " + val2);
        Console.WriteLine("Decimal 3 = " + val3);
        
        long res1 = Decimal.ToInt64(val1);
        long res2 = Decimal.ToInt64(val2);
        long res3 = Decimal.ToInt64(val3);
        
        Console.WriteLine("64-bit signed integer (value1) = " + res1);
        Console.WriteLine("64-bit signed integer (value2) = " + res2);
        Console.WriteLine("64-bit signed integer (value3) = " + res3);
    }
}

The output of the above code is −

Decimal 1 = 1.00
Decimal 2 = 34.678
Decimal 3 = 0.999
64-bit signed integer (value1) = 1
64-bit signed integer (value2) = 34
64-bit signed integer (value3) = 0

Exception Handling

Example

using System;

public class Demo {
    public static void Main() {
        try {
            Decimal validValue = 123.456m;
            Decimal largeValue = Decimal.MaxValue;
            
            Console.WriteLine("Converting valid decimal: " + validValue);
            long result1 = Decimal.ToInt64(validValue);
            Console.WriteLine("Result: " + result1);
            
            Console.WriteLine("\nAttempting to convert Decimal.MaxValue...");
            long result2 = Decimal.ToInt64(largeValue);
            Console.WriteLine("Result: " + result2);
        }
        catch (OverflowException ex) {
            Console.WriteLine("OverflowException: " + ex.Message);
        }
    }
}

The output of the above code is −

Converting valid decimal: 123.456
Result: 123

Attempting to convert Decimal.MaxValue...
OverflowException: Value was either too large or too small for an Int64.

Conclusion

The Decimal.ToInt64() method provides a straightforward way to convert decimal values to 64-bit signed integers by truncating the fractional part. Remember that it throws an OverflowException if the decimal value is outside the range of a long data type.

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

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements