How to pick the last digit of a number in Actionscript 3

Asked

Viewed 96 times

0

I have a variable with some value (var qtdd = 758) within a function and would like to take only the last digit of this variable, in case the number 8. Does anyone know how to do this in Actionscript 3?

Follows the code:

this.addEventListener("tick", capta_x.bind(this));

function capta_x() {

    var qtdd = 758;

}
  • Post the code to make it easy for anyone who can help you friend

  • I added. Obg friend.

2 answers

1


Divide the number by 10 and take only the rest of the division, but as the % serves precisely to return the rest of the division, the first calculation and unnecessary:

function capta_x() {
    var qtdd = 758;
    return qtdd % 10;
}
  • Solved! Thank you friend.

0

Another way, in AS3 :

var qtdd = 758;
var qtddString:String = qtdd.toString(); // converte para string
var ultimoChar = qtddString.charAt(qtddString.length - 1)); // pega o ultimo character e retorna "8"
var ultimoNum = Number(ultimoChar); // converte para numero e retorna 8

Browser other questions tagged

You are not signed in. Login or sign up in order to post.