| Author |
Converting double to int.
|
Haakon Lind
Greenhorn
Joined: Sep 18, 2002
Posts: 22
|
|
Hi! I`ve got an assignment wich causes me to find out if a number is a decimal or not, in short. So I+m thinkingof converting a double number to integer and see if the number changes. But I can`t get the conversion right, what am I doing wrong here ? Getting the error : double cannot be dereferenced. Is what I`m trying to do possible, is it the best way of doing it? Thanks
|
 |
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
|
|
|
"number" is a primitive. You cannot call a method on it using object syntax -- number.intValue().
|
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
|
 |
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
|
|
To convert a double to an int, just cast it: To convert back to an int, you can just assign, no cast needed:
|
 |
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
|
|
Originally posted by Haakon Lind: Hi! I`ve got an assignment wich causes me to find out if a number is a decimal or not, in short.
Try getting the value as a String, then parse it using StringTokenizer for the decimal point. Just a suggestion.
|
 |
Haakon Lind
Greenhorn
Joined: Sep 18, 2002
Posts: 22
|
|
Thanks Ron, I actually didn`t know that variables are primitive, i thought only direct input of char, int, double etc. was primitive. My Idea should work out now as it gave me what I expected. Thanks for input Anthony, but think that will require one or two more lines of code Thanks!
|
 |
 |
|
|
subject: Converting double to int.
|
|
|