ActionScript 3 Number to String Conversion
Tutorials

ActionScript 3 Number to String Conversion


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

If you you'd like to do ActionScript 3 Number to String conversions, then you can use the toString() method of the Number class.

Here is an example:
var myNumber:Number = 7;
var myString:String = myNumber.toString();
Here, we start off by creating a number - myNumber with a value of 7. In the next line, that number is converted into a string and is assigned to a variable named myString.

Why would I want to convert numbers to strings?
One example of how this can be useful is if you'd like to calculate some number value and then display it inside a text field.

For example:
var value1:Number = 7;
var value2:Number = 2;
var total:Number = value1 + value2;

myTextField.text = total.toString();
// Assume that myTextField is an instance of the TextField class
// and that it has already been created and added to the stage
Here, we're creating two numbers (7 and 2), which are then added together (which sums up to 9). The sum is then displayed inside a text field. Without the toString() method, Flash will give us an error message if we try to assign a Number value inside the text field. The error message will state:
1067: Implicit coercion of a value of type Number to an unrelated type String.
This means that we are trying to force a number to be a string. A text field can not contain Number data so we will have to convert it to a String instead. Although, 9 as a Number looks the same as "9" as a String, you must still explicitly differentiate between the two inside your ActionScript 3 code. This makes AS3 number to string conversions pretty useful.
 
If the number is a decimal (e.g. .5), Flash will add a leading 0 when it performs the number to string conversion (i.e. .5 will become "0.5").

Also, the toString() method of the Number class accepts one parameter for the radix. The radix lets you specify which numeric base (from 2 to 36) to use when the number is converted into a string. For example, if you'd like the number to be interpreted as an octal (base 8), then you pass a value of 8 to the radix parameter:
var myNumber:Number = 14;
trace( myNumber.toString(8) );
If we used base 10, then this will still output 14. But since we specified base 8 instead, this will output 16. If no radix is specified, a default value of 10 (decimal) is used.

So that is how you do ActionScript 3 number to string conversions.




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

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

- Creating Multiple Textfields And Positioning Them Using Arrays And For Loops In Flash Actionscript 3.0
In this tutorial, we are going to learn how to create multiple text fields and how to position them in different locations using an array and a for loop in Flash using ActionScript 3. Go ahead and create a new Flash ActionScript 3.0 document. Select frame...

- 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








.