C#中的Decimal.CompareTo()方法
C#中的Decimal.CompareTo()方法用于将此实例与指定的对象或Decimal进行比较,并返回其相对值的指示。
语法
以下是语法-
public int CompareTo (decimal value); public int CompareTo (object value);
示例
现在让我们看一个实现Decimal.CompareTo()方法的示例-
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))); } }
输出结果
这将产生以下输出-
Decimal 1 = 65.15 Decimal 2 = 65.15 Comparison Value = 0
示例
现在让我们来看另一个实现Decimal.CompareTo()方法的示例-
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))); } }
输出结果
这将产生以下输出-
Decimal 1 = 65.15 Decimal 2 = 414.286 Comparison Value = -1