[PLUG] OT: Formula For Determining # Of Digits In An Integer
Galen Seitz
galens at seitzassoc.com
Thu Feb 17 22:05:41 UTC 2005
Jason Van Cleve <jason at vancleve.com> wrote:
> Hi,
>
> I know many of you folks are advanced in mathematics. I need a formula
> that will take an integer and return the number of its digits:
>
> 3 --> 1
> 25 --> 2
> 52341 --> 5
> 032 --> 2 (ignore leading 0's)
>
> Any ideas?
Brute force?
#include <math.h>
int x, y;
y = floor(log10((double)x)) + 1;
or
int x, y;
y = 1;
while (x /= 10)
y++;
Untested, of course.
galen
More information about the PLUG
mailing list