C# Double Equals(Double)

Description

Double Equals(Double) returns a value indicating whether this instance and a specified Double object represent the same value.

Syntax

Double.Equals(Double) has the following syntax.


public bool Equals(
  double obj
)

Parameters

Double.Equals(Double) has the following parameters.

  • obj - A Double object to compare to this instance.

Returns

Double.Equals(Double) method returns true if obj is equal to this instance; otherwise, false.

Example

The following example reports that the Double value .333333 and the Double value returned by dividing 1 by 3 are unequal.


using System;/*from  w ww  . j a  v a 2  s  . c om*/

public class MainClass{
  public static void Main(String[] argv){  
    // Initialize two doubles with apparently identical values 
    double double1 = .33333;
    double double2 = 1/3;
    // Compare them for equality
    Console.WriteLine(double1.Equals(double2));    // displays false

  }
}

The code above generates the following result.

Example 2

The following example compares .33333 and 1/3, the two Double values that the previous code example found to be unequal. In this case, the values are equal.


using System;//w w w  .ja  v a  2  s  .  c om

public class MainClass{
  public static void Main(String[] argv){  
    // Initialize two doubles with apparently identical values 
    double double1 = .333333;
    double double2 = (double) 1/3;
    // Define the tolerance for variation in their values 
    double difference = Math.Abs(double1 * .00001);
    
    // Compare the values 
    // The output to the console indicates that the two values are equal 
    if (Math.Abs(double1 - double2) <= difference)
       Console.WriteLine("double1 and double2 are equal.");
    else
       Console.WriteLine("double1 and double2 are unequal.");

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version