Decimal.ToUInt32() Method in C#

The Decimal.ToUInt32() method in C# is used to convert the value of the specified Decimal to the equivalent 32-bit unsigned integer. This method performs truncation, discarding the fractional part and returning only the integer portion.

Syntax

Following is the syntax −

public static uint ToUInt32(decimal val);

Parameters

val − The decimal number to convert to a 32-bit unsigned integer.

Return Value

Returns a 32-bit unsigned integer (uint) that represents the truncated value of the specified decimal. The fractional part is discarded.

How It Works

The method converts a decimal value to uint by removing the decimal places. For example, 67.487m becomes 67, and 0.001m becomes 0. If the decimal value is negative or exceeds the range of uint (0 to 4,294,967,295), an OverflowException is thrown.

Decimal.ToUInt32() Conversion Process Decimal 67.487m Truncate decimal part uint 67

Example with Small Decimal Values

using System;
public class Demo {
   public static void Main(){
      Decimal val = 0.001m;
      Console.WriteLine("Decimal value = " + val);
      uint res = Decimal.ToUInt32(val);
      Console.WriteLine("32-bit unsigned integer = " + res);
   }
}

The output of the above code is −

Decimal value = 0.001
32-bit unsigned integer = 0

Example with Larger Decimal Values

using System;
public class Demo {
   public static void Main(){
      Decimal val = 67.487m;
      Console.WriteLine("Decimal value = " + val);
      uint res = Decimal.ToUInt32(val);
      Console.WriteLine("32-bit unsigned integer = " + res);
   }
}

The output of the above code is −

Decimal value = 67.487
32-bit unsigned integer = 67

Example with Exception Handling

using System;
public class Demo {
   public static void Main(){
      try {
         Decimal val1 = 4294967295.5m; // Within uint range
         Decimal val2 = -10.5m; // Negative value
         
         Console.WriteLine("Converting " + val1);
         uint res1 = Decimal.ToUInt32(val1);
         Console.WriteLine("Result: " + res1);
         
         Console.WriteLine("Converting " + val2);
         uint res2 = Decimal.ToUInt32(val2); // This will throw exception
      }
      catch (OverflowException ex) {
         Console.WriteLine("OverflowException: " + ex.Message);
      }
   }
}

The output of the above code is −

Converting 4294967295.5
Result: 4294967295
Converting -10.5
OverflowException: Value was either too large or too small for a UInt32.

Conclusion

The Decimal.ToUInt32() method converts decimal values to 32-bit unsigned integers by truncating the fractional part. It's useful for converting decimal values to whole numbers, but be aware that negative values or values exceeding the uint range will throw an OverflowException.

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

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements