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.

Syntax

Following is the syntax −

public static long ToInt64 (decimal val);

Above, Val is the decimal number to convert.

Example

Let us now see an example to implement the Decimal.ToInt64() method −

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) (Decimal to signed integer) = "+res1);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example to implement the Decimal.ToInt64() method −

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = 1.00m;
      Decimal val2 = 34.678m;
      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) (Decimal to signed integer) = "+res1);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

Decimal 1 = 1.00
Decimal 2 = 34.678
64-bit signed integer (value1) (Decimal to signed integer) = 1
64-bit signed integer (value1) (Decimal to signed integer) = 34

Updated on: 12-Nov-2019

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements