Reputation: 25
I have number 22 with me. I wanted to get the last digit of this number.
var no = 22;
How to get the last digit 2 from this number ?
Upvotes: 0
Views: 3421
Reputation: 11512
You can do it with the help of Modulus operator %
like:
var no = 22;
var lastDigit = no%10;
var cartno = 22;
var onesOnly = parseInt(cartno % 10);
alert(onesOnly);
Upvotes: 4
Reputation: 5532
Simply do:
var number = 22;
var lastdigit = number.toString().split('').pop();
alert(lastdigit);
that's it :)
Upvotes: 0
Reputation: 358
Use this JS function .charAt() you can use that to find the last digit. Credit to Kashyap How to get Last digit of number
var num = 22;
var str = num.toString();
var lastDigit = str.charAt(str.length-1);
alert(lastDigit);
Upvotes: 0
Reputation: 1047
var no = 22
var lastdigit = no % 10 #contains last digit
console.log(lastdigit)
Upvotes: 0