Digits In Number
Monkey Forums/Monkey Programming/Digits In Number
| ||
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 |
| ||
Convert it to a String. |
| ||
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 |
| ||
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? |
| ||
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 |
| ||
Thanks Warpy once again :) |