Results 1 to 5 of 5
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Exclamation 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.

    Thanks in advance

    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.
    Last edited by ozzyman; 03-13-2011 at 01:40 PM. Reason: Additional Info.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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,

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

    or

    Java Code:
    if (Math.abs(myValue - otherValue) <= DELTA) {
      // remove item
    }
    Last edited by Fubarable; 03-13-2011 at 01:52 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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,

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

    or

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Makes sense, thanks!!

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

Similar Threads

  1. Trimming trailing zeros in a byte
    By Aaron_Sharp in forum New To Java
    Replies: 1
    Last Post: 01-06-2011, 11:04 AM
  2. Trimming of array
    By Paulious1 in forum New To Java
    Replies: 0
    Last Post: 09-01-2010, 02:18 PM
  3. Trimming a String
    By PhilArmstrong in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 08:01 PM
  4. Replies: 3
    Last Post: 11-09-2009, 07:19 AM
  5. Trimming URL
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 03-10-2009, 05:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •