# Thread: Double to Int Issue

1. Member
Join Date
Nov 2010
Posts
15
Rep Power
0

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

Last edited by sunde887; 07-16-2011 at 05:54 AM.

3. Member
Join Date
Nov 2010
Posts
15
Rep Power
0
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. 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.

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

7. Member
Join Date
Nov 2010
Posts
15
Rep Power
0
Oh for heavens sake, I should have realized that! I was totally reading that incorrectly. Thanks for the help yall!

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

9. Member
Join Date
Nov 2010
Posts
15
Rep Power
0
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.

#### Posting Permissions

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