I have a number assigned to a variable, like that:
var myVar = 1234;
Now I want to get the second digit (2 in this case) from that number without converting it to a string first. Is that possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
So you want to get the second digit from the decimal writing of a number.
The simplest and most logical solution is to convert it to a string :
or
If you don’t want to do it the easy way, or if you want a more efficient solution, you can do that :
Demonstration
For people interested in performances, I made a jsperf. For random numbers using the log as I do is by far the fastest solution.