Help with trimming a Double value

• 03-13-2011, 02:38 PM
ozzyman
Help with trimming a Double value
I have a method which takes a double value, and then processes it to output various double values depending on the input. I then use another method to remove duplicates from the ArrayList because the first method often processes the same output twice or thrice. The process I use to remove duplicates usually works fine, but I was testing it with loads of random values as the input, and I noticed that in one case there were two values the same:

one was 7.30000000001
another 7.3

So the method that removes duplicates didnt remove one of them because of the minor difference. So now I want to know how I can round the double values to 2 decimal places, e.g 7.30 in this case, and then I can remove duplicates after rounding.

p.s. I know how to format strings e.g. %.2f but I want to actually edit the double value to process it, rather than altering only the output.
• 03-13-2011, 02:41 PM
Fubarable
There is no such thing as an exact double value (in any programming language). Your test for removal should use a very small double constant DELTA, and the item should be removed if it = another value +/- DELTA. In other words,

Code:

``` if (myValue <= otherValue + DELTA && myValue >= otherValue - DELTA) {   // remove item }```

or

Code:

```if (Math.abs(myValue - otherValue) <= DELTA) {   // remove item }```
• 03-13-2011, 03:16 PM
JosAH
Quote:

Originally Posted by Fubarable
There is no such thing as an exact double value (in any programming language). Your test for removal should use a very small double constant DELTA, and the item should be removed if it = another value +/- DELTA. In other words,

Code:

``` if (myValue <= otherValue + DELTA && myValue >= otherValue - DELTA) {   // remove item }```

or

Code:

```if (Math.abs(myValue - otherValue) <= DELTA) {   // remove item }```

Be careful with that approach, i.e. if those two values are very large you can consider them to be equal if they differ only a few 'ulps' (see the Math class for the definition of an ulp); i.o.w. their difference may never be smaller than DELTA but they are not equal. Another approach would be Math.abs(x/y-1)< DELTA

kind regards,

Jos
• 03-13-2011, 03:24 PM
Fubarable
Makes sense, thanks!!
• 03-13-2011, 03:37 PM
ozzyman
Thanks to both of you for the superfast replies.

In my case using this DELTA difference would work just fine as the end figure needs to be 2 decimal places. The only problem is that i've used a HashSet to remove the duplicates but it's fine, i'll just rewrite the method to remove duplicates manually.