If I have a number like 1234, how do I only pick 3?

in Engine

This question is very unclear. Do you want to pick a particular digit from a given 4-digit number? Does it have to be 4 digits, or can it be any number at all? Do you want to pick a random digit, or only the tens place? Please describe what you're trying to do in more detail.

Hi,
In the example you gave, you can get the digit you want doing something like this:

First, convert it to string, so you can get indexes:

``````var number = 1234
var nstr = str(number)
``````

then, get the index you want (remember indexes start in 0), and convert it back to int:

``````var digit = int(nstr[2])
``````

Then, do whatever you want with the digit:

``````print(digit)
``````

That should print 3. I assume there are other ways. Is that what you wanted?

by (3,505 points)
selected

Thank you very much

You are welcome. If this helped you, you may select it so others see its solved.

Hello
There is a way to do this backwards. The way the solution is now, it's counting like 1, 2, 3. There is a way to make it count like 4, 3