# Double to Int Issue

• 07-16-2011, 05:40 AM
phosphide
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.

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:

Quote:

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:

Code:

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

Any ideas?
• 07-16-2011, 05:49 AM
sunde887
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.

• 07-16-2011, 05:54 AM
phosphide
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.
• 07-16-2011, 06:01 AM
Fubarable
Quote:

Originally Posted by phosphide
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.

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,

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:
Code:

` int foo = 10.5.intValue();`
which makes no sense whatsoever.
• 07-16-2011, 06:02 AM
sunde887
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
• 07-16-2011, 06:02 AM
Fubarable
Quote:

Originally Posted by phosphide
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.
• 07-16-2011, 06:04 AM
phosphide
Oh for heavens sake, I should have realized that! I was totally reading that incorrectly. Thanks for the help yall!
• 07-16-2011, 08:04 AM
JosAH
Quote:

Originally Posted by phosphide
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
• 07-16-2011, 08:16 AM
phosphide
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.