Split number to it's digest or convert string to int

Godot Version

4.2

Question

is there a function that can split a number to it’s digest as in

toInt= 12.split("", true, 2)
print(toInt) # [1,2]

or can I convert a string to an int as it

toInt= "12".split("", true, 2)
print(toInt) # ["1","2"] <--- convert to int

Nope, but:

func number_to_digits(n):
	var digit_array = []
	for digit in str(n):
		if digit.is_valid_int():
			digit_array.push_back(digit.to_int())
	return digit_array

var number = 69
print(number_to_digits(number))
## => [6, 9]
2 Likes
toInt[0].to_int()
toInt[1].to_int()

I will have to save this for later, thanks Efi.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.