A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Math?

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    17

    Question Math?

    Hi, I have a project that allows someone to manage their budget. However i have 41 input text boxes and i cannot figure out how to add them together to one number and set the text of another text box to that number. I have tried everything. I do not see a math function for basic math. When i use google i get a list of math functions but never are basic math ( addition and subtraction). Does anyone know how i would do this? Thanks!

  2. #2
    Member
    Join Date
    Feb 2009
    Posts
    40
    Yea, you use - and +.

    var a
    var b
    var c

    c = a + b

    Your problem might be that you are trying to compare a string to an integer. You really should always post code when you have a question.

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    11
    Quote Originally Posted by MrStillwell View Post
    Your problem might be that you are trying to compare a string to an integer.
    Yeah, you'd need something like:
    c.text = (Number(a.text) + Number(b.text)) + ""; //The space just forces the end result to be a String.

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    17

    Lightbulb

    Quote Originally Posted by Scott64 View Post
    Yeah, you'd need something like:
    c.text = (Number(a.text) + Number(b.text)) + ""; //The space just forces the end result to be a String.
    If i did the first one, would i have to create a var for each text box?

    As for the second, what would i put where the number is. Can you explain it please.

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The first method won't really work for you - you need to sample the value of the text fields when you want to run the summation (rather than once at the beginning of the program).

    To use the second method, you can use the code exactly as Scott64 has it, just replace a, b, and c with the names of your textfields. The "Number" part is a code directive called casting - since the values are coming from textfields, the code needs a way to know that you want the text in those fields to be interpreted numerically instead of alphanumerically.

    PHP Code:
    "5"                    //  this is text
    5                        //  this is a number

    Number("5")            //  this is text being converted to a number - final output is 5
    String(5)                //  this is a number being converted to text - final output is "5"


    "5" "5"                //  this equals "55"
    5                    //  this equals 10

    Number("5") + 5        //  10
    "5"                //  "55"

    ("1" 2) + (2)     //  "123"
    String(3);    //  "6" 
    Please use [php] or [code] tags, and mark your threads resolved 8)

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    17

    Cow Icon

    i tried the second one, it set my c.text to NaN

    What went wrong. This is the current code i have.

    Please note i have 9 movieclip symbols, and each of them contain the textboxes.

    Code:

    finish.finBtn.addEventListener(MouseEvent.CLICK,Fi n)

    function Fin (event:MouseEvent):void{
    finish.spent.text = (Number(savings.savingsAccount.text))+(Number(savi ngs.governmentBonds.text));
    finish.left.text = (Number(finish.income.text))-(Number(finish.spent.text));
    }

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You got NaN in both finish.spent and finish.left? Try tracing out the values to make sure you're getting the right targets:

    PHP Code:
    trace(savings.savingsAccount.text ' = ' Number(savings.savingsAccount.text));
    trace(savings.governmentBonds.text ' = ' Number(savings.governmentBonds.text)); 
    Please use [php] or [code] tags, and mark your threads resolved 8)

  8. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    17

    Exclamation Uh ho?

    That didnt work either. And yes both are set to NaN. This did the same, well not exactly. It didnt even set my text. It just gave an error in my output panel :

    undefined = NaN
    undefined = NaN

Tags for this Thread

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