how to tell what a variable is

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

    how to tell what a variable is

    How can I tell if a variable. I want ot tell whether two variables are:

    1. Both numbers. Just a number. same as the number object in
    javascript.
    2. Either one is a string containing a number. For exmaple a
    string like "354" or "295.395".
    3. Either are strings containing a fraction. For example a string
    like "46/987".
    4. Either is a "fraction". By "fraction" I mean that it is either
    an array of two numbers or two number strings like in number 2.
    The first being the top and the second being the bottom. For
    example: [34,78] or ["34","57"].

  • RobG

    #2
    Re: how to tell what a variable is

    greenflame wrote:[color=blue]
    > How can I tell if a variable. I want ot tell whether two variables are:
    >
    > 1. Both numbers. Just a number. same as the number object in
    > javascript.[/color]

    To see if x is a number:

    var x = 5;
    if ( 'number' == typeof x ) // true

    var x = '5';
    if ( 'number' == typeof x ) // false

    Note that if your values come from form text elements, they are strings
    by default but may be converted to numbers.
    [color=blue]
    > 2. Either one is a string containing a number. For exmaple a
    > string like "354" or "295.395".[/color]

    I think you mean a string that can be converted to a number.

    var x = '354';
    if ( 'string' == typeof x ) // true

    However, this test is a bit useless - a regular expression provides a
    better solution:

    if ( /^\d+[.]?[\d*]?$/.test(x) ) {

    This will return true if x is one or more digits followed by an
    optional '.' followed by more optional digits. Anything else will
    return false. Note that this will rule out notation like 1.2e34 (which
    is otherwise perfectly valid as a number in JavaScript).

    Search the news group for examples of regular expression tests for
    number formats, there are plenty of examples.
    [color=blue]
    > 3. Either are strings containing a fraction. For example a string
    > like "46/987".[/color]

    var x = '46/987';
    if ( /\//.test(x) ) // true - string contains a '/' character

    But you probably need to detect that there is only one '/' character
    and that the stuff either side is a number.
    [color=blue]
    > 4. Either is a "fraction". By "fraction" I mean that it is either
    > an array of two numbers or two number strings like in number 2.
    > The first being the top and the second being the bottom. For
    > example: [34,78] or ["34","57"].
    >[/color]

    var x = '34,78';
    if ( /\,/.test(x) ) // true - string contains a ',' character

    *Note*
    Each of the above represent a trivial cases. There are other tests
    that likely should be conducted, such as checking whether the variable
    contains only digits, periods or commas, whether it contains multiple
    instances of those characters, what is an OK value and what isn't, etc.

    It is better to say how you are getting the input and what it is being
    used for so that appropriate tests can be suggested.


    --
    Rob

    Comment

    • Michael Winter

      #3
      Re: how to tell what a variable is

      On 24/06/2005 03:42, RobG wrote:
      [color=blue]
      > greenflame wrote:
      >[color=green]
      >> How can I tell if a variable. I want ot tell whether two variables are:
      >>
      >> 1. Both numbers. Just a number. same as the number object in
      >> javascript.[/color][/color]

      Just so you know, number objects are different from number values.

      var v = 5, // value
      o = new Number(5); // object

      You can perform arithmetic with both as the number object will be
      type-converted to a number first, but you cannot detect them in the same
      way. Rob's suggestion will work for values, whilst

      if(o && (Number == o.constructor))

      will work for numbers. If you don't actually care about objects, then
      never mind...

      [snip]
      [color=blue]
      > if ( /^\d+[.]?[\d*]?$/.test(x) ) {[/color]

      That's broken. For properly formatted decimal numbers,

      /^[+-]?(0|[1-9]\d*)(\.\d*)?$/

      is better.

      [snip]
      [color=blue][color=green]
      >> 3. Either are strings containing a fraction. For example a string
      >> like "46/987".[/color][/color]

      Presumably, the numerator and denominator should both be integers, and
      the denominator should not be zero?

      /^[+-]?(0|[1-9]\d*)\/[1-9]\d*$/
      [color=blue][color=green]
      >> 4. Either is a "fraction". By "fraction" I mean that it is either
      >> an array of two numbers or two number strings like in number 2.
      >> The first being the top and the second being the bottom. For
      >> example: [34,78] or ["34","57"].[/color][/color]

      Though an array would be more convenient, an object would be more
      appropriate. You could still use 0 and 1 as indices:

      {0 : 34, 1 : 78}

      or you could use named properties. If you used a constructor function,
      then testing is very simple:

      function Fraction(numera tor, denominator) {
      this.n = numerator; /* Or this[0] */
      this.d = denominator;
      }

      var myFraction = new Fraction(34, 78);

      if(myFraction && (Fraction == myFraction.cons tructor)) {
      /* Is a Fraction instance */
      }

      You could add checks into the constructor to ensure that the arguments
      are numbers, or number-like strings if you wanted to.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • greenflame

        #4
        Re: how to tell what a variable is

        Hmmm... thanks for the help. It seems I should learn about regular
        expressions. So what are they? Where can I learn all about them?

        Comment

        • greenflame

          #5
          Re: how to tell what a variable is

          Um.... sorry for posting twice but I for got to say somenting... Rob
          G.. your last way to check if a variable is of the format [324,72] or
          ["345","36"] doesnt actually test if it is of this format or atleast it
          doesnt seem to they way you made the code. I havent tryed it but it
          seems like it would only detect if the contents are of the form
          "543,876".

          Comment

          • greenflame

            #6
            Re: how to tell what a variable is

            Again sorry for posting what is now three straight posts. I was
            wondering like how to tell if a variable is a string and then convert
            to number. for example convert :

            1. "2" to 2
            2. "1.653" to 1.653
            3. "1.325e23" to 1.325e23
            4. "4.24e-4" to 4.24e-4

            I know this was partially covered above. Also I am probably having my
            question sort of answered twice.

            Comment

            Working...