aspose file tools*
The moose likes Beginning Java and the fly likes How to distinguish int , float , long and double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to distinguish int , float , long and double?" Watch "How to distinguish int , float , long and double?" New topic
Author

How to distinguish int , float , long and double?

magic zha
Greenhorn

Joined: Mar 25, 2005
Posts: 24
When i get a variable which may be int , float ,long or double,
how can i actually know what it is?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
magic zha
Greenhorn

Joined: Mar 25, 2005
Posts: 24
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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Mar 25, 2005
Posts: 24
Thank you!
Can you tell a simple way to check if it has a decimal part?
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: Mar 25, 2005
Posts: 24
Oh!i kown,thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to distinguish int , float , long and double?