What is Decimal.Equals method in C#?

Overview

The Decimal.Equals() method in C# is used to check if two decimals are equal or not.

Syntax

Decimal.Equals(d1, d2)

Parameters

  • d1: The first of the two decimal numbers we want to compare.

  • d2: The second of the two decimal numbers we want to compare.

Return value

It returns a boolean value. If both the specified decimal numbers are equal, true is returned. Otherwise, false is returned.

Example

// use system
using System;
// create class
class DecimalEquality
{
// main method
static void Main()
{
// compare decimal values
bool r1 = Decimal.Equals(23.5m, 34.57m);
bool r2 = Decimal.Equals(5.33334m, 0.1m);
bool r3 = Decimal.Equals(3.5m, 3.5m);
bool r4 = Decimal.Equals(12.3m, 12.3m);
// print results
Console.WriteLine(r1); // false
Console.WriteLine(r2); // false
Console.WriteLine(r3); // true
Console.WriteLine(r4); // true
}
}

Explanation

In the code above, we compare certain decimal values and print their results. The results show that r3 and r4 return true. Because the specified decimal numbers passed to the Decimal.Equals() method are equal.

Free Resources