Jump to content
  • Advertisement
Sign in to follow this  
abseeley

Python: Cutting up integers, quickly

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!