Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
communitycreator

What is Decimal.Equals method in C#?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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
}
}
Comparing decimal values if they are equal in C#

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.

RELATED TAGS

c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring