check Equal and Not Equal conditons for double values

14,529

Solution 1

If you're using a double (the primitive type) then a and b must not be equal.

double a = 1.0;
double b = 1.0;
System.out.println(a == b);

If .equals() works you're probably using the object wrapper type Double. Also, the equivalent of != with .equals() is

!a.equals(b)

Edit

Also,

else if ((a!=b||c!=d||e!=f))
{
  //My code here in case false condition
}

(Unless I'm missing something) should just be

else 
{
  //My code here in case false condition
}

if you really want invert your test conditions and test again,

 else if (!(a==b||c==d||e==f))

Or use De Morgan's Laws

 else if (a != b && c != d && e != f)

Solution 2

You can use

 !a.equals(b) || !c.equals(d) || !e.equals(f)

Well with double data type you can use

 ==,!= (you should try to use these first.. )
Share:
14,529
Siva
Author by

Siva

I am a passionate developer, love to code. Trying different languages (java,c#,android). Expertise in datawarehousing technologies and below tools. SAP Crystal Reports (v8.5,v10,v2008,v2013,v2016) SAP Business Objects (v3.x,v4.0) Tableau (v10.3,v2018.3.2) Tableau Data Prep (v2018.3.1) Full time freelance, please contact me for any freelancing jobs SOReadyToHelp

Updated on June 24, 2022

Comments

  • Siva
    Siva almost 2 years

    I am facing difficulty in comparing two double values using == and !=.

    I have created 6 double variables and trying to compare in If condition.

    double a,b,c,d,e,f;
    
    if((a==b||c==d||e==f))
    {
    //My code here in case of true condition
    }
    else if ((a!=b||c!=d||e!=f))
    {
    //My code here in case false condition
    }
    

    Though my condition is a and b are equal control is going to else if part

    So I have tried a.equals(b) for equal condition, Which is working fine for me.

    My query here is how can I check a not equal b. I have searched the web but I found only to use != but somehow this is not working for me.