Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to distinguish int , float , long and double?

 
magic zha
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i get a variable which may be int , float ,long or double,
how can i actually know what it is?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you manage to get a variable whose type you don't know? Decisions like that are made at compile time, not runtime.
[ April 11, 2005: Message edited by: Paul Sturrock ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to write a method that can take different kind of primitive types, you should take a look at the concept of overloading. Look at how System.println is implemented for different argument types, for example.
 
magic zha
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , i don't actually know what you mean.
For example, when the user enter in the instruction:"50 + 10" or "50.5 + 10",how can i tell the first number is an integer or a float?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parse them all as doubles to begin with. then see if any of them have any decimal digits - that is, if the integer part of them is equal to the entirety of them or not. if there aren't any decimals, it's an integer.
 
magic zha
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
Can you tell a simple way to check if it has a decimal part?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, if i were you, i'd check the API docs. there's a method or two in the class "Double" that might be useful for extracting the integer part of a Double, and then all you have to do is a quick comparison...
 
magic zha
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh!i kown,thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic