I am trying to compare two double values:
for(int i=0;i<count;i++){
Double i1=Double.parseDouble(pref.getString("longitude"+i, null));
Double i2=Double.parseDouble(pref.getString("latitude"+i,null));
Log.i("Longitude", i1+"");
Log.i("Latitude",i2+"");
Log.i("Longitude1",longitude+"");
Log.i("Latitude1", latitude+"");
Log.i("note",pref.getString("note"+i, null));
if(longitude==i1&&latitude==i2) {
String note=pref.getString("note"+i, null);
txt.setText(note);
}
}
There is one combination in shared preference that matches with longitude and latitude but in if when i compare its not assigning any value to textview txt.but in log there is a same value for latitude and latitude.can any one please tell whats wrong in this comparison why its not executing txt.settext statement?
I think there is a accuracy issue - types as float or double cannon be represented absolutely strictly. When I need to compare two doubles I use something like this
double doubleToCompare1 = some double value;
double doubleToCompare2 = another double value;
double EPS = 0.00001;
if(Math.abs(doubleToCompare1-doubleToCompare2)<EPS){
// assuming doubles are equals
}
EPS value depends on accuracy that you need. For coordinates as I remember it is 6 or 7 sign after comma.
Assuming latitude
and longitude
to be Double
too, try invoking the doubleValue
of both:
if(longitude.doubleValue() == i1.doubleValue() && latitude.doubleValue() == i2.doubleValue())
or just use equals
if(longitude.equals(i1) && latitude.equals(i2))
which derives in the first line, under the hood.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With