How do I find the ammount of digits in an int

Started by
12 comments, last by Brother Bob 21 years, 5 months ago
quote:
I recommend this way, although log(10) is 1, so you can safely ignore the dividing part.

Nope. log(10) is 2.3. log is the natural logarithm. But I agree this is the best way. Assumes n is an integer and larger than one though.

quote:
floor(log(n)/log(10)) + 1

Or ceil(log(n)/log(10)).
Advertisement
quote:Original post by Brother Bob
Or ceil(log(n)/log(10)).
Unfortunately, ceil(log(1000)/log(10)) == 3.
quote:Original post by Fruny
And yet, zero has one digit.
Yeah; it would be treated as a special case.
O.o

ln''s the natural logarithm, isn''t it? log could be any logarithm but when you don''t specify the base it''s presumed to be 10.
quote:
Unfortunately, ceil(log(1000)/log(10)) == 3.

True. Didn''t think about the case where log10(n) is an integer. My bad.
quote:
ln''s the natural logarithm, isn''t it? log could be any logarithm but when you don''t specify the base it''s presumed to be 10.

In mathematics, yes. But this is C++, and I suppose the standard says that log should return the natural logarithm.

This topic is closed to new replies.

Advertisement