Digits In Number

Monkey Forums/Monkey Programming/Digits In Number

Raz(Posted 2011) [#1]
How would I go about individually accessing each digit in a number so for example, the number 19132006

I want to loop through, 1, 9, 1, 3, 2, 0, 0, 6.

Any clues? :)

Thanks


Canardian(Posted 2011) [#2]
Convert it to a String.


Raz(Posted 2011) [#3]
I'm in the process of trying that, but all I seem to be able to return is the character code, not the actual digit


Raz(Posted 2011) [#4]
Best I've managed is

Local ScoreString:String = String(Score)
For Local i:Int = 0 to ScoreLength
	Local Digit:Int = Int(ScoreString[i..(i+1)])
Next


is that the most elegant way of doing it?


Warpy(Posted 2011) [#5]

Function getDigit(n,i)
	If i=0 Return n Mod 10
	Local e1 = Pow(10,i-1)
	Return ((n Mod (e1*10)) - (n Mod e1))/e1
End

Function Main()
	Local n = 123456789
	
	For Local i=1 To 9
		Print getDigit(n,i)
	Next
End



Raz(Posted 2011) [#6]
Thanks Warpy once again :)