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 Decimal to equivalent 8-bit unsigned integer in C#
To convert a Decimal value to an equivalent 8-bit unsigned integer (byte), C# provides the Decimal.ToByte() method. This method truncates the decimal portion and converts the integer part to a byte value, which ranges from 0 to 255.
The conversion follows banker's rounding rules for values exactly between two integers, and throws an OverflowException if the decimal value is outside the valid byte range.
Syntax
Following is the syntax for converting decimal to byte −
byte result = Decimal.ToByte(decimalValue);
Parameters
- decimalValue − The decimal number to convert to a byte. Must be between 0 and 255.
Return Value
Returns an 8-bit unsigned integer equivalent to the specified decimal value, with the fractional part truncated.
Using Decimal.ToByte() with Positive Values
Example
using System;
public class Demo {
public static void Main(){
Decimal val1 = 6.59m;
Decimal val2 = 30.12m;
Decimal val3 = 69.34m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Decimal 3 = "+val3);
byte res1 = Decimal.ToByte(val1);
byte res2 = Decimal.ToByte(val2);
byte res3 = Decimal.ToByte(val3);
Console.WriteLine("Byte value1 (Decimal to Byte) = "+res1);
Console.WriteLine("Byte value2 (Decimal to Byte) = "+res2);
Console.WriteLine("Byte value3 (Decimal to Byte) = "+res3);
}
}
The output of the above code is −
Decimal 1 = 6.59 Decimal 2 = 30.12 Decimal 3 = 69.34 Byte value1 (Decimal to Byte) = 6 Byte value2 (Decimal to Byte) = 30 Byte value3 (Decimal to Byte) = 69
Using Decimal.ToByte() with Negative Values
Example
using System;
public class Demo {
public static void Main(){
Decimal val1 = -0.22m;
Decimal val2 = -0.01m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
byte res1 = Decimal.ToByte(val1);
byte res2 = Decimal.ToByte(val2);
Console.WriteLine("Byte value1 (Decimal to Byte) = "+res1);
Console.WriteLine("Byte value2 (Decimal to Byte) = "+res2);
}
}
The output of the above code is −
Decimal 1 = -0.22 Decimal 2 = -0.01 Byte value1 (Decimal to Byte) = 0 Byte value2 (Decimal to Byte) = 0
Handling Banker's Rounding
The Decimal.ToByte() method uses banker's rounding (round to even) for values exactly halfway between two integers −
Example
using System;
public class Demo {
public static void Main(){
Decimal val1 = 2.5m; // rounds to 2 (even)
Decimal val2 = 3.5m; // rounds to 4 (even)
Decimal val3 = 4.6m; // truncates to 4
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Decimal 3 = "+val3);
byte res1 = Decimal.ToByte(val1);
byte res2 = Decimal.ToByte(val2);
byte res3 = Decimal.ToByte(val3);
Console.WriteLine("Byte value1 = "+res1);
Console.WriteLine("Byte value2 = "+res2);
Console.WriteLine("Byte value3 = "+res3);
}
}
The output of the above code is −
Decimal 1 = 2.5 Decimal 2 = 3.5 Decimal 3 = 4.6 Byte value1 = 2 Byte value2 = 4 Byte value3 = 4
Key Rules
-
Range Validation − The decimal value must be between 0 and 255, otherwise an
OverflowExceptionis thrown. - Fractional Part − The fractional part is rounded using banker's rounding rules.
-
Negative Values − Negative decimal values less than 0.5 convert to 0; values -0.5 or less throw an
OverflowException.
Conclusion
The Decimal.ToByte() method efficiently converts decimal values to 8-bit unsigned integers with built-in range checking. It handles fractional values using banker's rounding and ensures the result fits within the valid byte range of 0-255.
