Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the number of n digit in a 2+ digit number

For example, getting "5" in "256". The closest I've gotten is Math.floor(256/10)), but that'll still return the numbers in front. Is there any simple way to get what I want or would I have to make a big function for it? Also, for clarity: "n digit" would be defined. Example, getDigit(2,256) would return 5 (second digit)

like image 554
Anonymous Avatar asked Dec 23 '10 23:12

Anonymous


People also ask

How many digits are there in 2 digit number?

As we know any 2-digit number consists of two digits placed at its one's place and ten's place and these digits can be 0,1,2,3,4,5,6,7,8,9 (total 10 digits).

How many natural numbers are there containing 2 digits?

Hence, there are total 90 two-digit whole numbers.


1 Answers

Math.floor((256 / 10) % 10)

or more generally:

Math.floor(N / (Math.pow(10, n)) % 10)

where N is the number to be extracted, and n is the position of the digit. Note that this counts from 0 starting from the right (i.e., the least significant digit = 0), and doesn't account for invalid values of n.

like image 196
Dan Breslau Avatar answered Oct 29 '22 05:10

Dan Breslau