You can convert strings that are made up of numerical characters into actual Number data using the Number() constructor. The way it works is that you pass the String value to the Number() constructor, and in turn, this will create a Number version of the String that was passed to it.
The first line creates a string - myString with a value of 7. The second line takes that String data and passes it to the Number() constructor, which creates a number version from it. And the converted value, which is now of the Number data type, is assigned to the variable named myNumber.
var myString:String = "7"; var myNumber = Number(myString);
The syntax would be:
Why would I want to convert strings to numbers in AS3?
Let's say you have some String data made up of numerical characters, and you'd like to perform arithmetic operations using that data. If you don't convert the strings to numbers, then Flash won't allow you to perform arithmetic operations on them.
Here's an example. Let's say we have 2 strings that are made up of numerical characters:
And then we try to multiply them:
var s1:String = "2"; var s2:String = "5";
trace(s1 * s2);Flash is going to give us this error message:
1067: Implicit coercion of a value of type String to an unrelated type Number.This error is telling us that we are trying to force (or coerce) the String data to be used as Number data. You can only multiply numbers, NOT strings. So in the act of trying to multiply String data, we are implicitly coercing the values to change into Number data. We can't do that. We should explicitly convert the String data to Number data first, and then perform the arithmetic operation.
Here's another example. If we use the + operator on strings, then the strings will simply be joined together instead of the values being added to create a sum. Let's take a look:
Here, we have two strings - s1 = "2" and s2 = "5". If we were to use the + operator on these two values, the result would be 25. The values of s1 (2) and s2 (5) will simply be joined together in the same way that letters are joined together in order to make words. If these were actual Number data, then the result of adding 2 and 5 would be 7 instead.
var s1:String = "2"; var s2:String = "5"; trace(s1 + s2);
So if we want the values to be added to create a sum, then we'll need to use the Number() constructor to create number versions out of the strings:
Here, the strings s1 and s2 are each passed to the Number() constructor. This will create number versions from those strings, which can then be used in arithmetic operations. So here, the result will be 7, instead of the 25 we got from the previous example.
var s1:String = "2"; var s2:String = "5"; trace( Number(s1) + Number(s2) );
Here's another similar example. If you'd like to use the contents of a TextField instance in arithmetic operations, then you will also need to use the Number() constructor to create number values from those numerical characters inside the text field. Remember that characters inside a TextField are of the String data type, which is why we'll need to convert them to the Number data type for usage in arithmetic operations. For example:
So here, in the last line, we are retrieving the values inside the text fields (tf1 contains "2", tf2 contains "5"). Those values are passed to the Number() constructor which will create number versions from those values. Those numbers can then be used in the arithmetic operation. The result in this example would be 7.
// Assume that tf1 and tf2 are instances of the TextField class // and that they have already been created and added to the stage tf1.text = "2"; tf2.text = "5"; trace( Number(tf1.text) + Number(tf2.text) );
And that is how you can convert strings to numbers in ActionScript 3.