How to extract last(end) digit of the Number value using jquery. because i have to check the last digit of number is 0 or 5. so how to get last digit after decimal point
For Ex.
var test = 2354.55
Now how to get 5 from this numeric value using jquery.
i tried substr but that is only work for string not for Number format
Like if i am use var test = "2354.55";
then it will work but if i use var test = 2354.55
then it will not.
This worked for us:
Works for decimals:
Click on Run code snippet to verify.
Here is another one using
.slice()
:toString()
converts number to string, andcharAt()
gives you the character at a particular position.If you want the digit in the hundredths place, then you can do the following:
The problem with convert to string and getting the last digit is that it does not give the hundredths place value for whole numbers.
There is a JS function
.charAt()
you can use that to find the last digitTry this one:
Updates with ES6/ES2015:
We can use tagged template in such case as numbers are not iterable. So, we need to convert the number to a string representation of it. Then just spread it and get the last number popped.