Results 1 to 9 of 9
Thread: Double to Int Issue
- 07-16-2011, 05:40 AM #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.
But the output looks like this: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);
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:Years Between is: 1.73195546301E11
Round up is = 2147483647
But I was getting a "Double cannot be dereferenced" error.Java Code:int roundUp = yearsBetween.intValue();
Any ideas?
- 07-16-2011, 05:49 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.htmlLast edited by sunde887; 07-16-2011 at 05:54 AM.
- 07-16-2011, 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.
-
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,
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 roundUp = yearsBetween.intValue();
which makes no sense whatsoever.Java Code:int foo = 10.5.intValue();
- 07-16-2011, 06:02 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:04 AM #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!
- 07-16-2011, 08:04 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-16-2011, 08:16 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Check if double is double
By marshalthrone in forum New To JavaReplies: 8Last Post: 09-30-2009, 02:51 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double
By Xystus777 in forum New To JavaReplies: 2Last Post: 01-21-2009, 10:39 AM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks