# Python: Cutting up integers, quickly

This topic is 3885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Me again, I need to go through lots of integers and cut 'm up. Example: I have an integer equal to 234 myInt = 234 I need to work with each part of the integer individually therefore: myInt = 234 intHundreds = 2 intTens = 3 intOnes = 4 I know it is very easy to do a process like: myInt = str(myInt) #convert to string intHundreds = int(myInt.charAt(0)) #take the character in first position the '2' intTens = int(myInt.charAt(1)) #and so on intOnes = int(myInt.charAt(2)) But this brings up a huge speed problem in order to do this, involves a conversion from an int, to string, then back to int, and doing this about 47 thousand times isnt to quick as compared to what some other languages syntax is capable of. Im hoping to find maybe a charAt method for integers (charAt is actually from flash actionscript 1.0) or even some type of rounding method maybe? I dont really know what to expect but any input would be great. Thanks

##### Share on other sites
Look into the modulus operation.
intOnes = 234 % 10
See if you can figure the rest out based on that.

##### Share on other sites
how about just taking the remainder when you divide by 10?

ex. ones = myInt % 10;

##### Share on other sites
i figured mod would play a part in this i just couldnt grasp it, thanks for the quick response guys youve been a great help once again!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015825
×