📜  C#中的Double.CompareTo方法与示例

📅  最后修改于: 2021-05-29 18:10:21             🧑  作者: Mango

Double.CompareTo方法用于将当前实例与指定对象或Double对象进行比较。它将返回一个整数,该整数显示当前实例的值是大于,小于还是等于指定对象或Double对象的值。此方法的重载列表中有2种方法,如下所示:

  • CompareTo(Double)方法
  • CompareTo(Object)方法

Double.CompareTo(Double)方法

Double.CompareTo()方法用于将当前实例与指定的双精度浮点数进行比较,并返回一个整数,该整数显示此实例的值是否小于,等于或大于指定的值。双精度浮点数。

句法:

public int CompareTo (double value);

在这里,需要一个双精度浮点数进行比较。

返回值:该方法返回一个带符号的数字,指示此实例和value的相对值。

  • 小于零:此实例小于value或此实例不是数字(NaN)且value是数字。
  • 零:此实例等于value,或者此实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
  • 大于零:此实例大于值,或者此实例是数字,而值不是数字(NaN)。

下面的程序说明了Double.CompareTo(Double)方法的用法:

范例1:

// C# program to demonstrate the
// Double.CompareTo(Double)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        double value1 = 10d;
  
        // Declaring and initializing value2
        double value2 = 20d;
  
        // compare both double value
        // using CompareTo() method
        int status = value1.CompareTo(value2);
  
        // checking the status
        if (status > 0)
            Console.WriteLine("{0} is greater than {1}",
                                       value1, value2);
        else if (status < 0)
            Console.WriteLine("{0} is less than {1}",
                                     value1, value2);
        else
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
    }
}
输出:
10 is less than 20

范例2:

// C# program to demonstrate the
// Double.CompareTo(Double)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(5d, 7d);
        get(5.5d, 4.5d);
        get(10d, 20d);
        get(7.5d, 19.5d);
    }
  
    // defining get() method
    public static void get(double value1,
                           double value2)
    {
  
        // compare both double value
        // using CompareTo() method
        int status = value1.CompareTo(value2);
  
        // checking the status
        if (status > 0)
            Console.WriteLine("{0} is greater than {1}",
                                        value1, value2);
        else if (status < 0)
            Console.WriteLine("{0} is less than {1}",
                                     value1, value2);
        else
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
    }
}
输出:
5 is less than 7
5.5 is greater than 4.5
10 is less than 20
7.5 is less than 19.5

Double.CompareTo(Object)方法

Double.CompareTo()方法用于将当前实例与指定对象进行比较,并返回一个整数,该整数指示当前实例的值是否小于,等于或大于指定对象的值。

句法:

public int CompareTo (object value);

在这里,它需要一个对象进行比较,或者为null。

返回值:该方法返回一个带符号的数字,指示此实例和value的相对值。

  • 小于零:此实例小于value或此实例不是数字(NaN)且value是数字。
  • 零:此实例等于value,或者此实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
  • 大于零:此实例大于value或此实例是一个数字,而值不是一个数字(NaN)。

异常:如果value不是Double,则抛出ArgumentException。

下面的程序说明Double.CompareTo(Object)方法的用法:

范例1:

// C# program to demonstrate the
// Double.CompareTo(object)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            double value1 = 10d;
  
            // Declaring and initializing value2
            object value2 = 20d;
  
            // compare both double value
            // using CompareTo() method
            int status = value1.CompareTo(value2);
  
            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);
            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be double");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
10 is less than 20

示例2:对于ArgumentException

// C# program to demonstrate the
// Double.CompareTo(object)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            double value1 = 10d;
  
            // Declaring and initializing value2
            object value2 = 1 / 3;
  
            // compare both double value
            // using CompareTo() method
            int status = value1.CompareTo(value2);
  
            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);
  
            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be double");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
value2 must be double
Exception Thrown: System.ArgumentException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.double.compareto?view=netframework-4.7.2