C#中的Double.CompareTo()方法用于将此实例与指定对象或Double对象进行比较,并返回一个整数,该整数指示此实例的值是否小于,等于或大于指定对象的值。或Double对象。
语法如下-
public int CompareTo (double val); public int CompareTo (object val);
上面,第一种语法中的值val是要比较的双精度浮点数,而第二种语法中的val是要比较的对象。
现在让我们看一个例子-
using System; public class Demo { public static void Main() { double d1 = 150d; double d2 = 150d; Console.WriteLine("Double1 Value = "+d1); Console.WriteLine("Double2 Value = "+d2); Console.WriteLine("Are both the double values equal? = "+d1.CompareTo(d2)); } }
输出结果
这将产生以下输出-
Double1 Value = 150 Double2 Value = 150 Are both the double values equal? = 0
现在让我们来看另一个示例-
using System; public class Demo { public static void Main() { double d1 = 20d; object d2 = 20d; Console.WriteLine("Double1 Value = "+d1); Console.WriteLine("Double2 Value = "+d2); Console.WriteLine("Are both the double values equal? = "+d1.CompareTo(d2)); } }
输出结果
这将产生以下输出-
Double1 Value = 20 Double2 Value = 20 Are both the double values equal? = 0