Reputation: 468
I'm trying to execute a query in java, so that I will get some values from DB. I'm trying to compare the money/float value fetched from DB & this money/float is got from Result set as rs.getString. So while comparing, it is is matching. Here is my code, DBQuery is select * from Tablename where Price='value1' value1 is a predefined value getting from UI
ResultSet rs = stmt.executeQuery(DBquery+"'"+value1+"';");
z:
while (rs.next()){
String name= rs.getString(DBCol3);
System.out.println("name="+name);
System.out.println("value1="+value1);
if(value1.equalsIgnoreCase(name)){
System.out.println("Values are Same");
break z;
}else{
System.out.println("Values Mismatch");
}
}
in this case, i'm getting value1 from UI as 123.45 In db also, Price column is having the same value. But while comparing, it shows me as mismatching. name=123.45000
Upvotes: 0
Views: 1442
Reputation: 68715
Do not compare decimal values using string comparison. As in your two numbers are equal but due to the trailing zeros string comparison fails.
You should convert the string values to double to avoid mismatch due to trailing zeros. You can user Double.parseDouble(String)
for converting the string to double. Once you have two number compare them using ==
operator.
Upvotes: 1