mediaroot mediaroot - 5 months ago 8
Javascript Question

Javascript - How to get the last digit after decimal number?

e.g.,

var myNum = 1.208452


I need to get the last digit of myNum after decimal so it is (2)

Answer

You could try something like:

var temp = myNum.toString();
var lastNum = parseInt(temp[temp.length - 1]); // it's 2

Edit

You might want to check if your number is an actual decimal, you can do:

var temp = myNum.toString();
if(/\d+(\.\d+)?/.test(temp)) { 
    var lastNum = parseInt(temp[temp.length - 1]);

    // do the rest
}
Comments