d1 doesn't equal d2 - how to use a 'delta' to correct this?

Hi all,

quick question:

I have 2 doubles (amount money)

d1 = 722.0899999999999and

d2 = 722.09

obviously d1 is 722.09, but when I compare them d1.equals(d2) the return result is false (of course).

what's the best way to make sure that a delta of 0.001 is legit and the return result is true?

thanks!

[349 byte] By [xianwinwina] at [2007-11-27 8:41:08]
# 1

You can use some tolerance level.

if((d1 - d2) < tolerance) {

// numbers are effectively equal

}

All you need to do is determine what is an acceptable tolerance.

You would also need to use Math.abs()

Message was edited by:

flounder

floundera at 2007-7-12 20:39:57 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks!
xianwinwina at 2007-7-12 20:39:57 > top of Java-index,Java Essentials,Java Programming...