Suppose we have a real number...We dnt know what it was defined...How can we determine that it is float or double?
How To determine whether the number is float or double?
Collapse
X
-
-
Its not a String its a real number(already mentioned)..
Regarding idea --- I am receiving a real number from a function. I need to print what the received number is float or double ? We can determine by storing it in float and see the compiler error of incompatible types but I have to print that in a program? I dnt kw about the return type but know that it is a real number that is being returned....Comment
-
That is a silly requirement; if a method returns a double and you try to receive it in a float variable the compiler would complain. The other way around is silly as well (the compiler won't complaint) because you can know the return type of the method. The entire exercise is just silly and not worth this discussion.Its not a String its a real number(already mentioned)..
Regarding idea --- I am receiving a real number from a function. I need to print what the received number is float or double ? We can determine by storing it in float and see the compiler error of incompatible types but I have to print that in a program? I dnt kw about the return type but know that it is a real number that is being returned....
kind regards,
JosComment
-
Actualy the idea was based on isNumber function...So cant there be function like isFloat or isDoubleComment
-
What you want to use is Reflection.
Reflection looks at the data types. You can use it to determine what data type an Object is....You really should research this topic before you continue.
You can start by reading this article provided by Sun called Trail: The Reflection API. After that, if you're still confused by the topic, you should google it and then try it out.
If you have any problems with Reflection then get back to us :)
-FrinnyComment
-
-
I would imagine that the isNumber method would accept an Object as a parameter...and from that Object the OP could use reflection to figure out what Type of Object it is and return true/false, or print whatever they need to print accordingly.
-FrinnyComment
-
The OP probably didn't know what to pass the method.
:) instanceof Sounds cool :)
Didn't know there was such a method.
I'll check it out.Comment
-
Hmm...r035198x you might be right.
I'm not sure if this suggestion is even going to help the OP.
If the isNumber() method doesn't accept an Object then this whole suggestion won't work. I get the impression that the instanceof operator won't work with primitive data types (like float and double) because it compares Objects to Types. The OP would have to be looking for Double or Float Objects not primitive double or float types.
I can't see this working any other way though...
I guess we'll wait and see what the OP has to say about it :)
Thanks r035198x, I learned something new today :)
-Frinny
(I've been working too long in an environment where everything is an Object)Comment
Comment