e.g.,
var myNum = 1.208452
I need to get the last digit of myNum after decimal so it is (2)
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
}
This approach:
var regexp = /\..*(\d)$/;
var matches = "123.456".match(reg);
if (!matches) { alert ("no decimal point or following digits"); }
else alert(matches[1]);
How this works:
\. : matches decimal point
.* : matches anything following decimal point
(\d) : matches digit, and captures it
$ : matches end of string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With