isInteger, isFloat functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Alvarado

    isInteger, isFloat functions?

    Hello,
    Does anyone have handy functions for testing whether a text field value is

    a) a valid integer
    b) a valid floating point number?

    Thanks for any assistance, - Dave
  • RobG

    #2
    Re: isInteger, isFloat functions?

    D. Alvarado wrote:[color=blue]
    > Hello,
    > Does anyone have handy functions for testing whether a text field value is
    >
    > a) a valid integer
    > b) a valid floating point number?[/color]

    Try the FAQ:



    Follow the links for parseInt and parseFloat - choose your
    - poison MS or Netscape. I think the Netscape reference is
    better on this one. You may find isNaN useful too.

    Cheers, Rob.

    Comment

    • Fox

      #3
      Re: isInteger, isFloat functions?



      RobG wrote:[color=blue]
      > D. Alvarado wrote:
      >[color=green]
      >> Hello,
      >> Does anyone have handy functions for testing whether a text field
      >> value is
      >> a) a valid integer
      >> b) a valid floating point number?[/color]
      >
      >
      > Try the FAQ:
      >
      > http://www.jibbering.com/faq/#FAQ4_12
      >
      > Follow the links for parseInt and parseFloat - choose your
      > - poison MS or Netscape. I think the Netscape reference is
      > better on this one. You may find isNaN useful too.
      >
      > Cheers, Rob.[/color]

      parseInt and parseFloat will both accept non-numeric characters in a string and
      return the digits used up to the first non-digit character -- e.g.:

      parseInt("123ab c") will return 123 [a "true"]
      parseFloat("123 .2abc") will return 123.2 [also a "true"]

      technically, you cannot pass either of these to an "isInteger" and "isFloat" and
      receive a true -- they should both be false.


      Comment

      • Fox

        #4
        Re: isInteger, isFloat functions?



        D. Alvarado wrote:[color=blue]
        > Hello,
        > Does anyone have handy functions for testing whether a text field value is
        >
        > a) a valid integer[/color]

        function
        isInteger(s)
        {
        var n = trim(s);
        return n.length > 0 && !(/[^0-9]/).test(n);

        }
        [color=blue]
        > b) a valid floating point number?[/color]

        function
        isFloat(s)
        {
        var n = trim(s);
        return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
        }


        isFloat "qualifies" and differentiates from an integer by testing for a decimal
        point and at least one digit after it. Therefore, 123. will not qualify as a
        floating point number [nor an integer in isInteger] -- it is an invalid input.
        If you require a digit *before* the decimal point for a float, you'll have to
        adjust the regex.

        These routines merely test for *any* non-digit(/non-numeric) character
        (therefore the 'g' selector in the regex is not required).
        [note: empty strings will return a "false positive" - that's why testing for
        string length]

        To trim away any spaces before or after the string:

        function
        trim(s)
        {
        return s.replace(/^\s+|\s+$/g, "");
        }


        You can test for Integer OR Float with:

        function
        isNumber(s)
        {
        var n = trim(s);
        return n.length>0 && +n == n;
        }

        or you can use !isNaN(numstr) on a non-empty string. [!isNaN("") returns true
        and isNaN apparently trims strings of whitespace before testing]


        Comment

        • Evertjan.

          #5
          Re: isInteger, isFloat functions?

          D. Alvarado wrote on 20 sep 2004 in comp.lang.javas cript:[color=blue]
          > Does anyone have handy functions for testing whether a text field
          > value is
          >
          > a) a valid integer[/color]

          t = " -12.345"
          alert(!/\./.test(t) && +t==t)
          t = " -12345"
          alert(!/\./.test(t) && +t==t)
          [color=blue]
          > b) a valid floating point number?[/color]

          t = " -12.34.5"
          alert(+t==t)
          t = " -12.345"
          alert(+t==t)

          Shoot!


          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress,
          but let us keep the discussions in the newsgroup)

          Comment

          • Philip Ronan

            #6
            Re: isInteger, isFloat functions?

            On 20/9/04 10:54 am, Evertjan. wrote:
            [color=blue]
            > D. Alvarado wrote on 20 sep 2004 in comp.lang.javas cript:[color=green]
            >> Does anyone have handy functions for testing whether a text field
            >> value is
            >>
            >> a) a valid integer[/color]
            >
            > t = " -12.345"
            > alert(!/\./.test(t) && +t==t)
            > t = " -12345"
            > alert(!/\./.test(t) && +t==t)
            >[color=green]
            >> b) a valid floating point number?[/color]
            >
            > t = " -12.34.5"
            > alert(+t==t)
            > t = " -12.345"
            > alert(+t==t)
            >
            > Shoot!
            >[/color]

            <pedant>
            what about t = "1.2e+03"?
            </pedant>

            --
            Philip Ronan
            phil.ronanzzz@v irgin.net
            (Please remove the "z"s if replying by email)


            Comment

            • Evertjan.

              #7
              Re: isInteger, isFloat functions?

              Philip Ronan wrote on 20 sep 2004 in comp.lang.javas cript:[color=blue]
              > On 20/9/04 10:54 am, Evertjan. wrote:[color=green][color=darkred]
              >>> a) a valid integer[/color]
              >>
              >> t = " -12.345"
              >> alert(!/\./.test(t) && +t==t)
              >> t = " -12345"
              >> alert(!/\./.test(t) && +t==t)
              >>[/color][/color]
              [..][color=blue]
              >
              > <pedant>
              > what about t = "1.2e+03"?
              > </pedant>
              >[/color]

              t = "1.2e+03"
              alert(!/\./.test(+t) && +t==t)
              t = "5.0000"
              alert(!/\./.test(+t) && +t==t)

              will take care of those.

              However, it will miserably fail here:

              t = "1.2e+33"
              alert(!/\./.test(+t) && +t==t)

              Though technically incorrect [John?],
              are such big/small numbers important to the OP ?


              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress,
              but let us keep the discussions in the newsgroup)

              Comment

              • Philip Ronan

                #8
                Re: isInteger, isFloat functions?

                On 20/9/04 11:15 am, Evertjan. wrote:
                [color=blue]
                > t = "1.2e+03"
                > alert(!/\./.test(+t) && +t==t)
                > t = "5.0000"
                > alert(!/\./.test(+t) && +t==t)
                >
                > will take care of those.
                >
                > However, it will miserably fail here:
                >
                > t = "1.2e+33"
                > alert(!/\./.test(+t) && +t==t)[/color]

                It will also fail when t = infinity (e.g., "t=1/0")

                How about:

                alert(!/\./.test(+t) && +t==t && (t+1)!=t)

                --
                Philip Ronan
                phil.ronanzzz@v irgin.net
                (Please remove the "z"s if replying by email)


                Comment

                • Fred Oz

                  #9
                  Re: isInteger, isFloat functions?

                  Fox wrote:

                  [color=blue]
                  > technically, you cannot pass either of these to an "isInteger" and
                  > "isFloat" and receive a true -- they should both be false.
                  >
                  >[/color]

                  I think Rob is on the right track (but could have offered some
                  code...)

                  Testing strings using regular expressions to see if they have
                  decimal places (and how many), surplus characters, scientific
                  notation, etc. requires exhaustive testing... let JS do
                  the work.

                  How about:

                  var x = someValue;
                  if (x == parseInt(x) && x == parseFloat(x)) {
                  alert(x + ' is a int');
                  } else if (x == parseFloat(x)) {
                  alert(x + ' is a float');
                  } else {
                  alert(x + ' is not a number I like...');
                  }

                  You can even use scientific notation if you like, so 1.2e+03 is
                  recognised as a float quite happily without complex pattern
                  matching.

                  Cheers, Fred

                  Comment

                  • Evertjan.

                    #10
                    Re: isInteger, isFloat functions?

                    Philip Ronan wrote on 20 sep 2004 in comp.lang.javas cript:[color=blue]
                    > It will also fail when t = infinity (e.g., "t=1/0")
                    >
                    > How about:
                    >
                    > alert(!/\./.test(+t) && +t==t && (t+1)!=t)[/color]

                    I don't think infinity is an integer but I wouldn't mind if it is.

                    (+t+1)!=+t ?

                    --
                    Evertjan.
                    The Netherlands.
                    (Please change the x'es to dots in my emailaddress,
                    but let us keep the discussions in the newsgroup)

                    Comment

                    • Fred Oz

                      #11
                      Re: isInteger, isFloat functions?

                      Philip Ronan wrote:

                      [color=blue]
                      > It will also fail when t = infinity (e.g., "t=1/0")
                      >[/color]

                      1/0 is not infinity, is it "undefined"- as is the result
                      of any division by zero.

                      Cheers, Fred.

                      Comment

                      • Philip Ronan

                        #12
                        Re: isInteger, isFloat functions?

                        On 20/9/04 12:44 pm, Fred Oz wrote:
                        [color=blue]
                        > Philip Ronan wrote:
                        >
                        >[color=green]
                        >> It will also fail when t = infinity (e.g., "t=1/0")
                        >>[/color]
                        >
                        > 1/0 is not infinity, is it "undefined"- as is the result
                        > of any division by zero.
                        >
                        > Cheers, Fred.[/color]

                        Really?
                        javascript:aler t(1/0) returns "Inf" in IE.

                        --
                        Philip Ronan
                        phil.ronanzzz@v irgin.net
                        (Please remove the "z"s if replying by email)


                        Comment

                        • Fred Oz

                          #13
                          Re: isInteger, isFloat functions?

                          Philip Ronan wrote:

                          [color=blue]
                          > Really?
                          > javascript:aler t(1/0) returns "Inf" in IE.
                          >[/color]

                          And now IE is an authority on mathematics? Heaven forbid!

                          Please read here:

                          A delightful site for writers and lovers of words and language. It is all about the number zero, its place in the history, philosophy, and world literatures. We have heard of calling someone 'a total zero' as an insult, but what does 'zero' really mean? This thoughtful and informative Website from Dr. Hossein Arsham, Wright Distinguished Research Professor of Statistics and Management Science at the University of Baltimore, discusses the history and philosophy of zero. Items presented in this site include arguments for and against dividing by zero, the ideas of zero as a void and as a number, zero in limits, square roots, and divergent series and floating points. The text is peppered with links to related mathematics, computer science, and history of mathematics sites. The Zero Saga is a good read for mathematicians, college and graduate level students in mathematics, or anyone interested in logic. A discussion of the mathematical problems associated with the number zero. The purpose of this page is to raise students and teachers awareness of issues with zero and other numbers. Imprecise mathematical thinking is by no means unknown. But, argues Dr. Arsham, we need to think more clearly if we are to keep out of trouble. This site is entertaining and educational, two concepts that need to go together in math education. The Menu has links to quickly bring the viewer to the desirable section. Scrolling through the document is also an option Presents a historical and cultural views of Zero including detailed account of the Islamic Culture to its major development and its influence into European Culture which enabled scientists to do computations which were not possible with the dominated Roman numbers with serious drawbacks including lack of zero.


                          Have a good one, Fred.

                          Comment

                          • Andrew Thompson

                            #14
                            Re: isInteger, isFloat functions?

                            On Mon, 20 Sep 2004 13:12:39 +0100, Philip Ronan wrote:[color=blue]
                            > On 20/9/04 12:44 pm, Fred Oz wrote:[color=green]
                            >> Philip Ronan wrote:
                            >>[color=darkred]
                            >>> It will also fail when t = infinity (e.g., "t=1/0")[/color][/color][/color]
                            ...[color=blue][color=green]
                            >> 1/0 is not infinity, is it "undefined"- as is the result
                            >> of any division by zero.[/color][/color]
                            ...[color=blue]
                            > javascript:aler t(1/0) returns "Inf" in IE.[/color]

                            All three of IE 6.0026, Moz. 1.7 and Opera 6.54
                            give consistent results for me..

                            javascript:aler t(1/0) -> 'Infinity'
                            javascript:aler t(0/0) -> 'NaN'
                            javascript:aler t(-1/0) -> '-Infinity'

                            Makes perfect sense to me.

                            --
                            Andrew Thompson
                            http://www.PhySci.org/codes/ Web & IT Help
                            http://www.PhySci.org/ Open-source software suite
                            http://www.1point1C.org/ Science & Technology
                            http://www.lensescapes.com/ Images that escape the mundane

                            Comment

                            • Evertjan.

                              #15
                              Re: isInteger, isFloat functions?

                              Andrew Thompson wrote on 20 sep 2004 in comp.lang.javas cript:
                              [color=blue]
                              > All three of IE 6.0026, Moz. 1.7 and Opera 6.54
                              > give consistent results for me..
                              >
                              > javascript:aler t(1/0) -> 'Infinity'
                              > javascript:aler t(0/0) -> 'NaN'
                              > javascript:aler t(-1/0) -> '-Infinity'
                              >
                              > Makes perfect sense to me.
                              >[/color]

                              And which of them are integers ?
                              ;-}

                              --
                              Evertjan.
                              The Netherlands.
                              (Please change the x'es to dots in my emailaddress,
                              but let us keep the discussions in the newsgroup)

                              Comment

                              Working...