What is the Decimal.ToByte() method in C#?

Overview

The Decimal.ToByte() method converts a specified decimal value to its equivalent 8-bit unsigned integer.

Syntax

public static byte ToByte (decimal d);

Parameters

  • d: The decimal number to convert.

Return value

The toByte() method returns the 8-bit unsigned integer equivalent of the decimal number d.

Code example

// use System
using System;
// create class
class HelloWorld
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 23.45m;
decimal d2 = 0.45m;
decimal d3 = 6.44m;
// get the bytes
Console.WriteLine(Decimal.ToByte(d1));
Console.WriteLine(Decimal.ToByte(d2));
Console.WriteLine(Decimal.ToByte(d3));
}
}

Explanation

  • Lines 11-13: We create decimal numbers whose 8-bit unsigned equivalent values we want to get.

  • Lines 16-18: We call the Decimal.Byte() method on the decimal values we created and print the results to the console.

Free Resources