genaray
genaray

Reputation: 1180

Comparing float and double for exact values with objects only

Im running into a problem where i need to have some sort of exact value comparison of two objects. The problem here is i have no idea in which sequence ill receive them... and both are passed as pure java objects, no primitives.


public void compare(Object floatOrDouble, Object floatOrDouble){
  // How do we compare ? 
}

How would we compare a float object and a double object for exact values without knowing which one is which ? Is there already some sort of util for such cases ? Do we need to check their classes manually ?

Upvotes: 1

Views: 43

Answers (1)

Ahmed Ibrahim
Ahmed Ibrahim

Reputation: 266

You can convert 2 objects to Double then you can compare. but you can't cast an object to a Double if the object is not a Double.

So you can convert 2 objects to String then to Double using new Double(object.toString()); then you can compare safely

Upvotes: 2

Related Questions