Reputation:
Okay so what I'm trying to do in AS3 is get the first number of a big number.
For example if I have a variable named TesterNum and it equals to 6:4204542 I'm trying to get the first number of the variable, which is 6.
I'm trying to create a code that says:
var TesterNum:Number = 6:4204542;
if(TheFirstNumberOfTesterNum == 6){
trace("Works");
}
What's the code to get the first number of a variable?
Upvotes: 0
Views: 332
Reputation: 1648
Use chatAt(); method like this:
var TesterNum:Number = 6:4204542;
var control:String = TesterNum.charAt(0);
trace(control);
//it give you 6
Upvotes: 0
Reputation: 14406
I'm going to assume that your variable is a String and not a Number (because of the colon which would cause your code in your question to throw a compiler error).
In that case, there are a few ways to do the same thing:
Use parseInt
- which will grab the whole number prior to anything that is not a number, so in your case, anything before the colon. This will be the easiest if your. first number can be more than one digit.
var TesterNum = "6:4204542";
if(parseInt(TesterNum) == 6) //true
Use charAt
, which get's the character at the specified index, then convert that character to an integer. This method is alright if you only need one digit ever.
var TesterNum = "6:4204542";
if(int(TesterNum.charAt(0)) == 6) //true
Use substr
to get all characters before the colon, then convert that to a Number or integer. This is exactly the same result as method #1.
int(TesterNum.substr(0, TesterNum.indexOf(":"))) == 6
Upvotes: 2