Decimal.ToUInt16() Method in C#

The Decimal.ToUInt16() method in C# is used to convert the value of the specified Decimal to the equivalent 16-bit unsigned integer (ushort). This method performs a narrowing conversion, truncating any fractional part.

Syntax

Following is the syntax −

public static ushort ToUInt16(decimal val);

Parameters

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

Return Value

Returns a ushort value that represents the converted decimal. The fractional part is truncated (not rounded).

How It Works

The method truncates the decimal value to the nearest integer toward zero. The valid range for ushort is 0 to 65,535. If the decimal value is outside this range, an OverflowException is thrown.

Decimal to UInt16 Conversion Decimal 875.647m truncate UInt16 875 Fractional part (.647) is removed

Example with Fractional Decimal

Let us see an example that demonstrates truncation of fractional values −

using System;
public class Demo {
   public static void Main(){
      Decimal val = 875.647m;
      Console.WriteLine("Decimal value = " + val);
      ushort res = Decimal.ToUInt16(val);
      Console.WriteLine("16-bit unsigned integer = " + res);
   }
}

The output of the above code is −

Decimal value = 875.647
16-bit unsigned integer = 875

Example with Small Decimal Values

This example shows how small decimal values are handled −

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

The output of the above code is −

Decimal value = 0.001
16-bit unsigned integer = 0

Example with Range Testing

This example demonstrates conversion with boundary values −

using System;
public class Demo {
   public static void Main(){
      Decimal[] values = {0m, 65535m, 32767.99m};
      
      foreach(Decimal val in values) {
         Console.WriteLine("Decimal: " + val);
         ushort result = Decimal.ToUInt16(val);
         Console.WriteLine("UInt16: " + result);
         Console.WriteLine();
      }
   }
}

The output of the above code is −

Decimal: 0
UInt16: 0

Decimal: 65535
UInt16: 65535

Decimal: 32767.99
UInt16: 32767

Key Rules

  • The decimal value must be between 0 and 65,535 (inclusive) to avoid OverflowException.

  • Fractional parts are truncated, not rounded.

  • Negative decimal values will throw an OverflowException.

Conclusion

The Decimal.ToUInt16() method converts decimal values to 16-bit unsigned integers by truncating the fractional part. It's useful when you need to extract the integer portion of a decimal value within the valid ushort range (0 to 65,535).

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

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements