I am entering the digits in the text field as 1528799
, can any one tell me how to get last 2 digits of the given number using jquery or javascript
To get the last digit of a number:Convert the number to a string and call the slice() method on the string, passing it -1 as a parameter. The slice method will return the last character in the string. Convert the string back to a number to get the last digit.
Last two digits of a number is basically the tens place and units place digit of that number. So given a number say 1439, the last two digits of this number are 3 and 9, which is pretty straight forward.
To get the last two characters of a string, call the slice() method, passing it -2 as a parameter. The slice method will return a new string containing the last two characters of the original string. Copied!
Just modulo it with 100
parseInt(document.getElementById("inputField").value, 10) % 100
For example
console.log(1528799 % 100);
will print 99
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