mediaroot mediaroot - 1 year ago 138
Javascript Question

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


var myNum = 1.208452

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

Answer Source

You could try something like:

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


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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download