A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Variable data type headache

  1. #1
    Junior Member
    Join Date
    Dec 2000
    Posts
    2
    I'm trying to add (the values of) two variables, both of which I've initialised as number data types (I think), but they simply concatenate when I do

    answer = variable1+variable2

    answer is also defined as a number data type. I can multiply, divide or subtract them but try and add them, and they just string together into a long number.
    What the mufugger am I missing

  2. #2
    Member
    Join Date
    Jun 2001
    Posts
    70
    How are you instantiating variable1 and variable2?

    Like this:
    variable1=5;
    variable2=5;

    or like this:
    variable1="5";
    variable2="5";

    The + is also used to concatenate two strings together as well as add two numbers. This is called an "overloaded operator" because the operator + can handle more than one operation. So if you instantiated your variables as strings with quotation marks, then Flash will treat them as strings and join them together.

  3. #3
    Junior Member
    Join Date
    Aug 2001
    Posts
    5
    If you are using Flash 5, then you need to do this:

    Code:
    variable1 = "5";
    variable2 = "3";
    answer = Number(variable1) + Number(variable2);
    For newbies, use of the Number() is to call a variable in "number form". If you put:

    Code:
    answer = variable1 + variable2;
    Then your results will be 53 instead 8.

    This is due to Flash 5's new variable handling. Without the use of Number(), then flash will think you are trying to call variable1 and variable2 as a "string", or "text" in another words. So it returns 53 instead of the correct number, 8.

    Hope that helps...

  4. #4
    Junior Member
    Join Date
    Jun 2001
    Posts
    24
    just as a side note I suppose; but Flash 5 AS will use the default formatting of a variable as it's initializtion.

    therefore if you setup
    Code:
    variable1 = 5;
    variable2 = "644";
    variable3 = ["3", 2];
    variable4 = true;
    you would have variable1 is a number type, variable2 is a string, variable3 is an array containing both types, and variable4 is a boolean.

    there is no need to declare the variables types in this scripting language, especially when you don't have to handle the stack yourself.

    just ditch the quotation marks if it's a number.

  5. #5
    Ive been doing this and get the nasty old NaN
    even though the variable reads -1000

    surely if this was a string it wouldnt have any problems interpreting it as a number....

    thanks

  6. #6
    Member
    Join Date
    Jun 2001
    Posts
    70
    There also exists another function called parseInt(expression, radix);

    radix
    is actually an optional arguement.

    But in any case:

    parseInt("411") == 411
    parseInt("4foo") == 4

    You can also convert hexadecimal, binary, and octal to integers.

    Look it up.

  7. #7
    hmmmmm... excellent
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center