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
Decimal.ToByte() Method in C#
The Decimal.ToByte() method in C# converts a decimal value to its equivalent 8-bit unsigned integer (byte). This method truncates the decimal portion and returns only the integer part. The byte data type can store values from 0 to 255.
Syntax
Following is the syntax −
public static byte ToByte(decimal val);
Parameters
val − The decimal number to convert to a byte value.
Return Value
Returns a byte value that represents the truncated integer portion of the specified decimal. If the decimal is negative, it rounds toward zero.
How It Works
The method performs truncation, not rounding. For example, 6.99m becomes 6, and -0.22m becomes 0. If the decimal value is outside the range of 0-255, an OverflowException is thrown.
Example 1 - Basic Conversion
Let us see an example demonstrating basic decimal to byte conversion −
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
Example 2 - Negative Values
Let us see how the method handles negative decimal values −
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
Example 3 - Boundary Values
Let us see how the method works with byte boundary values −
using System;
public class Demo {
public static void Main() {
Decimal val1 = 0.99m;
Decimal val2 = 255.99m;
Decimal val3 = 128.5m;
Console.WriteLine("Decimal values:");
Console.WriteLine(val1 + " -> " + Decimal.ToByte(val1));
Console.WriteLine(val2 + " -> " + Decimal.ToByte(val2));
Console.WriteLine(val3 + " -> " + Decimal.ToByte(val3));
}
}
The output of the above code is −
Decimal values: 0.99 -> 0 255.99 -> 255 128.5 -> 128
Key Points
| Aspect | Details |
|---|---|
| Range | Input must be between 0 and 255 (inclusive) |
| Conversion Type | Truncation (not rounding) |
| Negative Values | Values between -1 and 0 become 0 |
| Exception | OverflowException if value < 0 or > 255 |
Conclusion
The Decimal.ToByte() method provides a straightforward way to convert decimal values to bytes by truncating the fractional part. Remember that the method throws an exception for values outside the 0-255 range, making it essential to validate input values before conversion.
