How To determine whether the number is float or double?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    How To determine whether the number is float or double?

    Suppose we have a real number...We dnt know what it was defined...How can we determine that it is float or double?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Where do we have it? In a String?
    Give more details and also explain what you have tried or what your ideas are about it.

    Comment

    • puneetsardana88
      New Member
      • Aug 2009
      • 57

      #3
      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

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by puneetsardana88
        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....
        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.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          So what language is this hypothetical function written in? It can't be Java because there is no real number data type.

          Comment

          • puneetsardana88
            New Member
            • Aug 2009
            • 57

            #6
            Actualy the idea was based on isNumber function...So cant there be function like isFloat or isDouble

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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 :)

              -Frinny

              Comment

              • puneetsardana88
                New Member
                • Aug 2009
                • 57

                #8
                Thanks every1 ..I will study this and will get back to you incase of any problems....

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Reflection doesn't have much to do with it at all.
                  The question that the OP needs to answer is what would the isNumber function be accepting? The question as put isn't really making much sense.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    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.

                    -Frinny

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Safe bet but the OP didn't specify. In that case, if the type passed is Object then instanceof will do the job without using reflection.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Originally posted by r035198x
                        Safe bet but the OP didn't specify. In that case, if the type passed is Object then instanceof will do the job without using reflection.
                        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

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          It's an operator.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Well that would explain why I can't seem to find it :)

                            Hehe, should have just tried google first

                            [edit] found it :) [/edit]

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              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

                              Working...