User Guide

+ (addition) 71
Usage 3: Variables associated with dynamic and input text fields have the data type String. In the
following example, the variable
deposit is an input text field on the Stage. After a user enters a
deposit amount, the script attempts to add
deposit to oldBalance. However, because deposit
is a String data type, the script concatenates (combines to form one string) the variable values
rather than summing them.
var oldBalance:Number = 1345.23;
var currentBalance = deposit_txt.text + oldBalance;
trace(currentBalance);
For example, if a user enters 475 in the deposit text field, the trace() statement sends the value
4751345.23 to the Output panel.
To correct this, use the
Number() function to convert the string to a number, as in the following:
var oldBalance:Number = 1345.23;
var currentBalance:Number = Number(deposit_txt.text) + oldBalance;
trace(currentBalance);
The following example shows how numeric sums to the right of a string expression are not
calculated:
var a:String = 3 + 10 + "asdf";
trace(a); // 13asdf
var b:String = "asdf" + 3 + 10;
trace(b); // asdf310