anu
anu

Reputation: 25

How to get the last digit of a number in jquery?

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

Answers (5)

vijayP
vijayP

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

Amrinder Singh
Amrinder Singh

Reputation: 5532

Simply do:

var number = 22;
var lastdigit = number.toString().split('').pop();
alert(lastdigit);

that's it :)

Upvotes: 0

Mark Valenzuela
Mark Valenzuela

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

Ankush G
Ankush G

Reputation: 1081

var no = 22;
console.log(parseInt(no%10));

Upvotes: 0

Rehan Shikkalgar
Rehan Shikkalgar

Reputation: 1047

var no = 22
var lastdigit = no % 10 #contains last digit

console.log(lastdigit)

Upvotes: 0

Related Questions