Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert the value of the specified Decimal to the equivalent 16-bit unsigned integer in C#
The Decimal.ToUInt16() method in C# converts a decimal value to its equivalent 16-bit unsigned integer (ushort). This method performs truncation, discarding the fractional part and keeping only the integer portion of the decimal value.
Syntax
Following is the syntax for the Decimal.ToUInt16() method −
public static ushort ToUInt16(decimal value)
Parameters
-
value − The decimal number to be converted to a 16-bit unsigned integer.
Return Value
Returns a ushort (16-bit unsigned integer) that represents the truncated value of the specified decimal. The valid range is 0 to 65,535.
Converting Decimal with Fractional Part
When a decimal has a fractional part, the ToUInt16() method truncates (not rounds) the value −
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
Converting Small Decimal Values
Decimal values less than 1.0 will result in 0 when converted to ushort −
using System;
public class Demo {
public static void Main() {
Decimal val1 = 0.001m;
Decimal val2 = 0.999m;
Console.WriteLine("Decimal value 1 = " + val1);
Console.WriteLine("16-bit unsigned integer 1 = " + Decimal.ToUInt16(val1));
Console.WriteLine("Decimal value 2 = " + val2);
Console.WriteLine("16-bit unsigned integer 2 = " + Decimal.ToUInt16(val2));
}
}
The output of the above code is −
Decimal value 1 = 0.001 16-bit unsigned integer 1 = 0 Decimal value 2 = 0.999 16-bit unsigned integer 2 = 0
Handling Out of Range Values
If the decimal value is outside the range of ushort (0 to 65,535), an OverflowException will be thrown −
using System;
public class Demo {
public static void Main() {
try {
Decimal val = 100000m; // exceeds ushort.MaxValue (65535)
Console.WriteLine("Decimal value = " + val);
ushort res = Decimal.ToUInt16(val);
Console.WriteLine("16-bit unsigned integer = " + res);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: " + ex.Message);
}
}
}
The output of the above code is −
Decimal value = 100000 OverflowException: Value was either too large or too small for a UInt16.
Comparison: Truncation vs Rounding
| Method | Behavior | Example: 875.647m |
|---|---|---|
| Decimal.ToUInt16() | Truncates fractional part | 875 |
| Math.Round() then cast | Rounds to nearest integer | 876 |
Conclusion
The Decimal.ToUInt16() method converts a decimal to a 16-bit unsigned integer by truncating the fractional part. It throws an OverflowException if the value is outside the valid ushort range of 0 to 65,535.
