當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Double.CompareTo用法及代碼示例


Double.CompareTo方法用於將當前實例與指定對象或Double對象進行比較。它將返回一個整數,該整數顯示當前實例的值是大於,小於還是等於指定對象或Double對象的值。此方法的重載列表中有2種方法,如下所示:

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

Double.CompareTo(Double) Method

Double.CompareTo()方法用於將當前實例與指定的雙精度浮點數進行比較,並返回一個整數,該整數顯示此實例的值是小於,等於還是大於指定的double值。精度浮點數。


用法:

public int CompareTo (double value);

在這裏,需要一個雙精度浮點數進行比較。

返回值:此方法返回一個帶符號的數字,指示該實例和值的相對值。

  • 小於零:此實例小於value或該實例不是數字(NaN)並且value是數字。
  • 零:此實例等於value,或者此實例和value都不是數字(NaN),PositiveInfinity或NegativeInfinity。
  • 大於零:此實例大於value或此實例是一個數字,而value不是一個數字(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) Method

Double.CompareTo()方法用於將當前實例與指定對象進行比較,並返回一個整數,該整數指示當前實例的值是否小於,等於或大於指定對象的值。

用法:


public int CompareTo (object value);

在這裏,它需要一個對象進行比較,或者為null。

返回值:此方法返回一個帶符號的數字,指示該實例和值的相對值。

  • 小於零:此實例小於value或該實例不是數字(NaN)並且value是數字。
  • 零:此實例等於value,或者此實例和value都不是數字(NaN),PositiveInfinity或NegativeInfinity。
  • 大於零:此實例大於value或此實例是一個數字,而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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Double.CompareTo Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。