Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

This question is a bit basic, but how do I check the number of digits in an integer?

I need to check if an integer P is between 100 and 0 without doing an if condition simliar to (P>9)&&(P<100)?

Something like P%100==0? (where % is mod)

Thanks

share|improve this question
    
I think one of the most straightforward ways would be to check the rest of the division first by 10, then by 100, etc, until you get rest == 0. Then you count how many times you got a rest that was not 0, and that is your number of digits. There might be a smtarter way of checking it thought. –  Leonardo Fontoura Apr 23 '11 at 5:34
    
Out of curiosity: What is the motivation for this question? –  t.b. Apr 23 '11 at 5:35
    
@all: I edited the tags but wasn't sure which one to use in this case –  t.b. Apr 23 '11 at 5:41
    
@Leonardo Fontoura: Thank you @Theo Buehler: A part of a C++ program to building a wavefront planner for a course in robotics. –  morgoth_bauglir Apr 23 '11 at 6:52
    
You can check if P%100 == P; that will be "yes" for $0\leq P\leq 99$, but no for $P\lt 0$ and $p\geq 100$. If you want to include $100$, then do P%101==P. –  Arturo Magidin Apr 23 '11 at 7:13

1 Answer 1

up vote 4 down vote accepted

If you know you have a positive integer $n$, $\lfloor\log_{10}n\rfloor+1$ (that is, floor(log10(n))+1) will give the number of digits in $n$.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.