How to convert decimal values to 32-bit unsigned integers in C#

Overview

The Decimal.ToUInt32() method is used to convert a decimal value to its equivalent 32-bit unsigned integer in C#.

Syntax

public static ushort ToUInt32 (decimal d);

Parameters

  • d: The decimal value we want to convert to a 32-bit unsigned integer.

Return value

The return value is an UInt32, a 32-bit unsigned integer that is the equivalent of d.

Example

// use System
using System;
// create class
class DecimalTOUInt32
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 34.5m;
decimal d2 = 2.4356345m;
decimal d3 = 10.34m;
// convert to UInt32
// and print value
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToUInt32(d1),
Decimal.ToUInt32(d1).GetType());
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToUInt32(d2),
Decimal.ToUInt32(d2).GetType());
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToUInt32(d3),
Decimal.ToUInt32(d3).GetType());
}
}

Explanation

  • Lines 10–12: We create decimal variables d1, d2, and d3, and initialize them with decimal values.

  • Lines 16–26: We use the Decimal.ToUInt32() method to convert each value to an unsigned 32-bit integer and print the results.

Free Resources