A Flash Developer Resource Site

1. ## 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. 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. Originally Posted by MrStillwell
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. Originally Posted by Scott64
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. 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 + 5                    //  this equals 10 Number("5") + 5        //  10 5 + "5"                //  "55" ("1" + 2) + (1 + 2)     //  "123" String(1 + 2 + 3);    //  "6"  ```

6. 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:

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. 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));  ```

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

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap