Results 1 to 9 of 9
  1. #1
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Double to Int Issue

    Hi all, I cannot believe I am having such an issue with this. I am trying to convert a double to an integer with the following code.

    Java Code:
    double yearsBetween = (date2.getTime() - date1.getTime())/365;
    
    System.out.println("Years Between is: "+yearsBetween);
    
    int roundUp = (int)yearsBetween;
    
    System.out.println("Round up is = "+roundUp);
    But the output looks like this:

    Years Between is: 1.73195546301E11
    Round up is = 2147483647
    How the heck is that turning into 2147483647? Am I doing something wrong here? I know it will actually round down but I am going to add 1 which is why it is called round up. I also tried:

    Java Code:
    int roundUp = yearsBetween.intValue();
    But I was getting a "Double cannot be dereferenced" error.

    Any ideas?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The "double cannot be dereferenced" stems from calling a method on a double, which don't exist for primitives. The other problem looks like you are putting a very large value into a smaller value, ints are 32 bits, so the max is about 2 billion, and -2 billion is the min.

    Doubles are 64 bits and can hold much larger values. See if casting to long helps, it can hold 64 bits I believe, which (off the top of my head) I believe is ~9 trillion.

    http://download.oracle.com/javase/tu...datatypes.html
    Last edited by sunde887; 07-16-2011 at 06:54 AM.

  3. #3
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Uh, I'm not using any large numbers. The value of the double comes out to 1.73 and it should round to 1 when I convert it to an integer. It is somehow converting to 2147483647.

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

    Default

    Quote Originally Posted by phosphide View Post
    Hi all, I cannot believe I am having such an issue with this. I am trying to convert a double to an integer with the following code.

    Java Code:
    double yearsBetween = (date2.getTime() - date1.getTime())/365;
    You know of course that Date#getTime() returns a result representing milliseconds, not days, and so dividing that difference by 365 will return a number that is not close to representing a year by a couple orders of magnitude.

    And here,

    Java Code:
    int roundUp = yearsBetween.intValue();
    as sunde states, you're trying to call a method on a double, a primitive. That's like trying to do this:
    Java Code:
     int foo = 10.5.intValue();
    which makes no sense whatsoever.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your output is: 1.73195546301E11 which is pretty large, unless I'm having a brain fart that number is equivalent to
    173,195,546,301

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

    Default

    Quote Originally Posted by phosphide View Post
    Uh, I'm not using any large numbers. The value of the double comes out to 1.73 and it should round to 1 when I convert it to an integer. It is somehow converting to 2147483647.
    LOL, look again. That's 1.73 x 10 to the 11th power! A very big number indeed.

  7. #7
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Oh for heavens sake, I should have realized that! I was totally reading that incorrectly. Thanks for the help yall!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by phosphide View Post
    Oh for heavens sake, I should have realized that! I was totally reading that incorrectly. Thanks for the help yall!
    According to the IEEE/754 (floating point formats) specification, when you cast a floating point number to an integer and it is too big, the resulting int value should be the largest representable int number; 2147483647 == 0x7FFFFFFF (this is Integer.MAX_VALUE).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Well, the reason why I was screwing up my calculations was because I (for some reason) was looking at 1.73....e14 as 1.73 carried out to 14 decimals. When in fact that number was in scientific notation.

    Though, your response does explain why I got the number that I did. Appreciate it.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  2. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. Double
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 11:39 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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