# How to extract digits within a number

Hi everyone,

I want to take a number, from 0 to 999, and extract the units, tens and hundreds.

Right now, I can do it but it's probably too complicated:

I compare to 900. if smaller, then I compare to 800 and so on. Once I figure out the hundreds, I do a Modulo, to get the tens.

then the process starts again: I compare to 90, if smaller, Then I compare to 80....Again, I do a Modulo, to get the units....

I'm pretty sure there is a "cleaner" way to do it, but I can't think of any !

With integer math, you don’t get any fractional part or remainder when you divide. So a simple divide by ten works. In concert with the Modulo operator, it’s easy to pull out digits.

``````678 % 10 = 8

678 / 10 = 67

67 % 10 = 7

67 / 10 = 6

6 % 10 = 6

6 / 10 = 0  // Stop when you see a zero
``````

Delta_G: Thank for the clue !

Sometime when searching for a solution, we ignore the easy way, and concentrate on a way too complicated alternative !

I completely forgot that the Integer round-down the result ! Was Soooooo simple !!!

Ps991: next time you have a comment like that, you can roll it tight and ...well you know the rest !

Im just saying if this guy bothered to look anywhere on the known internet, he could have found that answer. For example, typing in the exact name of this thread into google yields the same answer you gave him in THE VERY FIRST HIT!
Hell, if he looked on just the forum here, he would find like 20 threads asking the same question.

Little effort questions get little effort responses…

For example, this thread which was started just a few weeks ago, containing the most efficient way to extract digits from a number.

EDIT: Removed helpful link due to his previous comment