• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing an Int

 
Kassi Hill
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping someone could tell me if you could parse a long into an int and if you can, how you do it??
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java in General (Beginner) Forum...
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean something like this:
long lg = 5000;
int i = (int)lg;
All you need to do is cast the long into an int. If the long is too long for the int then you will get a wrong result.
 
Kassi Hill
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kassi, Thomas is correct in that you only need to perform a cast to convert the number. He also points out the downside to the "narrowing conversion".
If you are concerned that the number is converted correctly, you can check that the long value is not greater than Integer.MAX_VALUE before attempting the cast, or you can verify that i == lg after the conversion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic