0 votes

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

in Engine by (17 points)

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.

1 Answer

+2 votes
Best answer

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 by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.