02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Converting Strings to Numbers

How do we take that string and let Flash know we actually want it to be a number? The answer is simple: learn a new function. This function type is called Casting (a term we will revisit in the future.)

After seeing the previous video, you might get confused about why we used a number in double quotes, wanting it to behave as string. We sometimes need the number to behave as string when the number is to be attached along with strings. This is rare when working in Flash but becomes very common when working with external teams or back-end code. Most of the time, we don't specify a number as string but it may appear in double quotes or single quotes if it's downloaded from some other source or from the server directly.

In these cases, we have the number as string but we want it to be an integer for addition. In this instance, Flash does some automatic conversion. If you leave the conversion for the application, it will be invisible to you %u2013 you might convert something without wanting to or knowing about it. This is because basically Flash tries to convert everything into strings when one string is added to a number. So to convert the number within quotes into an integer, we make use of the keyword Number. We enclose the number along with the double quotes in a bracket and type Number before it. This converts the data inside the bracket from string to integer. Hence, both the numbers are considered as numbers thereafter and addition of the numbers is performed accordingly.

A simple example will let you know how exactly it's done. If the line from the source is trace(2 '3'), the output is 23 %u2013 but we want it to be the sum of those two numbers. A few modifications are done as said earlier such as trace(2 Number('3')). Therefore, when the program is now compiled, the application adds both numbers and gives the output as 5 which is the preferred answer (mathematically correct) for most users. We therefore recommend not to allow Flash to perform automatic conversions; instead use these various techniques and keywords to make the conversions according to our will.

'I love being Prime'

Have a question but don't want to ask it publicly? If you're a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Video Transcript

00:00Try to avoid these automatic conversions, and we're going to show you how we could convert ourselves and force flash to treat something that we think is a number to force it to be a number.
00:13So let's see how we could do that. For example, if we do one more trace right now,
00:18and we want to do 2 plus 2 again, and this time around we have a string now,
00:23and I have to say one thing, even though now it doesn't seem logical, why would you use a string instead of a number?
00:29Sometimes you're going to get a string instead of a number, such as if you're getting data from an external source that you don't control,
00:36such as from a server. Sometimes the information that you're going to get is not going to be a numeric value.
00:42So if you don't make sure your value is numeric, flash might convert it, not in the way you want it.
00:48We could call a new function that we haven't used yet, which is called number,
00:53and wrap it with a round bracket around the string 2.
01:00By doing that, we're basically telling flash, take this string and convert it into a number.
01:06And if we run that application one more time, we're going to see that the last trace comes out as 4,
01:12because now our string was converted into a number before we did the addition.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!