Decimal.ToUInt64() Method in C#

The Decimal.ToUInt64() method in C# converts a decimal value to a 64-bit unsigned integer (ulong). This method performs truncation, meaning it removes the fractional part and returns only the integer portion of the decimal number.

Syntax

Following is the syntax −

public static ulong ToUInt64(decimal value);

Parameters

  • value − The decimal number to convert to a 64-bit unsigned integer.

Return Value

Returns a ulong (64-bit unsigned integer) that represents the truncated value of the specified decimal.

Decimal to UInt64 Conversion Decimal 99.75m UInt64 99 ToUInt64() Fractional part is truncated

Using Decimal.ToUInt64() with Fractional Values

When the decimal has a fractional part, the method truncates it and returns only the integer portion −

using System;

public class Demo {
   public static void Main() {
      Decimal val = 99.29m;
      Console.WriteLine("Decimal value = " + val);
      ulong res = Decimal.ToUInt64(val);
      Console.WriteLine("64-bit unsigned integer = " + res);
   }
}

The output of the above code is −

Decimal value = 99.29
64-bit unsigned integer = 99

Using Decimal.ToUInt64() with Small Decimal Values

When the decimal value is less than 1, the method returns 0 since the integer part is 0 −

using System;

public class Demo {
   public static void Main() {
      Decimal val = 0.001m;
      Console.WriteLine("Decimal value = " + val);
      ulong res = Decimal.ToUInt64(val);
      Console.WriteLine("64-bit unsigned integer = " + res);
   }
}

The output of the above code is −

Decimal value = 0.001
64-bit unsigned integer = 0

Using Decimal.ToUInt64() with Large Values

The method can handle large decimal values within the ulong range −

using System;

public class Demo {
   public static void Main() {
      Decimal val1 = 1000000.99m;
      Decimal val2 = 18446744073709551615m; // Max ulong value
      
      Console.WriteLine("Decimal value 1 = " + val1);
      Console.WriteLine("Converted to UInt64 = " + Decimal.ToUInt64(val1));
      
      Console.WriteLine("Decimal value 2 = " + val2);
      Console.WriteLine("Converted to UInt64 = " + Decimal.ToUInt64(val2));
   }
}

The output of the above code is −

Decimal value 1 = 1000000.99
Converted to UInt64 = 1000000
Decimal value 2 = 18446744073709551615
Converted to UInt64 = 18446744073709551615

Exception Handling

The method throws an OverflowException if the decimal value is negative or exceeds the maximum value of ulong

using System;

public class Demo {
   public static void Main() {
      try {
         Decimal negativeVal = -10.5m;
         Console.WriteLine("Attempting to convert: " + negativeVal);
         ulong result = Decimal.ToUInt64(negativeVal);
      }
      catch (OverflowException ex) {
         Console.WriteLine("OverflowException: " + ex.Message);
      }
   }
}

The output of the above code is −

Attempting to convert: -10.5
OverflowException: Value was either too large or too small for a UInt64.

Conclusion

The Decimal.ToUInt64() method converts decimal values to 64-bit unsigned integers by truncating the fractional part. It throws an OverflowException for negative values or values exceeding the ulong range, making it essential to handle potential exceptions in real-world applications.

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

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements