ActionScript 3: Convert Strings to Numbers
Tutorials

ActionScript 3: Convert Strings to Numbers


In this lesson, we are going to learn how to convert strings to numbers in AS3.

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.

Example:
var myString:String = "7";
var myNumber = Number(myString);
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.

The syntax would be:
Number(String)

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:
var s1:String = "2";
var s2:String = "5";
And then we try to multiply them:
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:
var s1:String = "2";
var s2:String = "5";
trace(s1 + s2);
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.

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:
var s1:String = "2";
var s2:String = "5";
trace( Number(s1) + Number(s2) );
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.

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:
// 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) );
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.

And that is how you can convert strings to numbers in ActionScript 3.




- As3 Countdown Timer - Creating A Simple Countdown Timer In Flash Using Actionscript 3
In this tutorial, we'll learn how to create a very simple AS3 countdown timer in Flash. Step 1 Create a new Flash ActionScript 3 document. Select the text tool once your document opens up. Step 2 Go to the Properties Inspector and choose the following...

- Expressions And Operators In Actionscript 3
An expression is something that computes or evaluates into a single value. In a very simple form, a reference to a variable is already an expression. For example: var quantity:Number = 5; trace(quantity); Here, the variable quantity evaluates to a value...

- Part 2 - Variable Naming Rules And Conventions - Introduction To Flash As3 Variables
PART 1: Creating ActionScript 3 Variables and Assigning Values to themPART 2: Variable Naming Rules and ConventionsPART 3: Fun with Variables - Complete the Story Variable names are author-defined. This means that you, as the author of the Flash ActionScript...

- Part 1 - Creating Actionscript 3 Variables And Assigning Values To Them - Introduction To Flash As3 Variables
PART 1: Creating ActionScript 3 Variables and Assigning Values to themPART 2: Variable Naming Rules and ConventionsPART 3: Fun with Variables - Complete the Story Welcome to this tutorial series on an Introduction to Flash AS3 Variables. In part 1 of...

- How To Create A Quiz In Flash - Actionscript 3 Tutorial - Part 2
[Read PART 1 here] In the first part of this lesson, we added the functionality that will allow the user to take the quiz - we added the questions and the correct answers, and we enabled the user to submit his or her own answers by using the input field...



Tutorials








.