How do I get a specific number?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jeff_Andonuts

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

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.

kidscancode | 2019-01-07 17:09

:bust_in_silhouette: Reply From: p7f

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:


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

Thank you very much

Jeff_Andonuts | 2019-01-07 18:26

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

p7f | 2019-01-07 18:29

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

Augusto Santana | 2022-09-02 02:26