• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing double values...

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we compare a double value...
double d = 999.99;
if(d==999.99)
{
}
Or should we comvert the double to string,
Double dd=new Double(999.99);
String str="9999999.99";
if(str.equalsIgnoreCase(dd.toString()))
{
}
Which way is correct?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with doubles and floats is that there is always this rounding issue with them. Therefore you can never COUNT on the value of the double being what you think it should be.
Try reading this: http://www.javaranch.com/ubb/Forum1/HTML/000525.html
 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to check the input value is exact 999.99 or not.
So there no change in this value.
So can chack the incomming value aganist this hard coded value as I discribled above?
Thanks
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Bala Krisna
U can compare this as follows
class Test1{
void testDouble(){
double d=999.99;
double dd = 999.99;
// or
//U can get a value by input and change it to double
//check with API Double methods.
if(d==999.99)
System.out.println("I am equal and double value");
else
System.out.println(" I am not equal");
}
public static void main(String[]args){
Test1 t= new Test1();
t.testDouble();
}
}
regrds
vkswami
 
reply
    Bookmark Topic Watch Topic
  • New Topic