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


C# Double.Equals()用法及代碼示例


Double.Equals()方法用於獲取一個值,該值指示Double的兩個實例是否表示相同的值。此方法的重載列表中共有兩種方法,如下所示:

    • 等於(雙精度)方法
    • 等於(對象)方法

Double.Equals(Double)

此方法用於返回一個值,該值指示此實例和指定的Double對象是否表示相同的值。


用法: public bool Equals (double obj);
Here, it takes a Double object to compare to this instance.

返回值:如果obj等於此實例,則此方法返回true;否則,返回true。否則為假。

下麵的程序說明Double.Equals()方法的使用:

示例1:

// C# program to demonstrate the 
// Double.Equals(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 Equals(Double) method 
        bool status = value1.Equals(value2); 
  
        // checking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}", 
                                    value1, value2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                        value1, value2); 
    } 
}
輸出:
10 is not equal to 20

示例2:

// C# program to demonstrate the 
// Double.Equals(Double) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // calling get() method 
        get(5d, 5d); 
        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 Equals(Double) method 
        bool status = value1.Equals(value2); 
  
        // cheking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}",  
                                    value1, value2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                        value1, value2); 
    } 
}
輸出:
5 is equal to 5
5.5 is not equal to 4.5
10 is not equal to 20
7.5 is not equal to 19.5

Double.Equals(Object) Method

此方法用於返回一個值,該值指示此實例是否等於指定的對象。

用法: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.

返回值:如果obj是Double的一個實例,並且等於該實例的值,則此方法返回true;否則,此方法返回true。否則為假。


以下示例程序旨在說明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Double.Equals(Object) 
// 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 
        object value2 = 1 / 36; 
  
        // compare both double value 
        // using Equals(object) method 
        bool status = value1.Equals(value2); 
  
        // checking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}",  
                                   value1, value2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                        value1, value2); 
    } 
}
輸出:
10 is not equal to 0

示例2:

// C# program to demonstrate the 
// Double.Equals(object) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(5d, 1 / 3); 
        get(5.5d, 1 / 3); 
        get(10d, 1 / 24); 
        get(7.5d, 2 / 5); 
    } 
  
    // defining get() method 
    public static void get(double value1, object value2) 
    { 
  
        // compare both double value 
        // using Equals(object) method 
        bool status = value1.Equals(value2); 
  
        // checking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}", 
                                    value1, value2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                        value1, value2); 
    } 
}
輸出:
5 is not equal to 0
5.5 is not equal to 0
10 is not equal to 0
7.5 is not equal to 0

參考:



相關用法


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