![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decimal.CompareTo() Method in C#
The Decimal.CompareTo() method in C# is used to compare this instance to a specified object or Decimal and returns an indication of their relative values.
Syntax
Following is the syntax −
public int CompareTo (decimal value); public int CompareTo (object value);
Example
Let us now see an example to implement the Decimal.CompareTo() method −
using System; public class Demo { public static void Main(){ Decimal val1 = 65.15m; Decimal val2 = 65.15m; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Console.WriteLine("Comparison Value = "+(val1.CompareTo(val2))); } }
Output
This will produce the following output −
Decimal 1 = 65.15 Decimal 2 = 65.15 Comparison Value = 0
Example
Let us now see another example to implement the Decimal.CompareTo() method −
using System; public class Demo { public static void Main(){ Decimal val1 = 65.15m; object val2 = (decimal)4.1428600E+2; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Console.WriteLine("Comparison Value = "+(val1.CompareTo(val2))); } }
Output
This will produce the following output −
Decimal 1 = 65.15 Decimal 2 = 414.286 Comparison Value = -1
Advertisements