This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting double to int.

 
Haakon Lind
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"number" is a primitive. You cannot call a method on it using object syntax -- number.intValue().
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic