Archived

This topic is now archived and is closed to further replies.

leggyguy

counting how many digits in a number?

Recommended Posts

I am currently using something like int a, digits; if (a < 10) digits = 1; else if (a < 100) digits = 2; etc etc upto 10 digits. There must be a tidier way. (I am using this so that the figures are printed in a certain place depending on amount of digits in the number.)

Share this post


Link to post
Share on other sites
using properties of log you can accomplish this
#include <math.h>

int digitsInNumber(int _in)
{
return (int(log(_in)/log(10))+1);
}
int main()
{
int number;
cout << "enter the number" << endl;
cin >> number;
cout << "that number has " << digitsInNumber(number) << " digits"<}

Share this post


Link to post
Share on other sites
Well if you consider the way the log funcion really looks its still untidy (and a lot slower too I guess)

edit:

I'd use something like this:

  
int digitsOf(int number)
{
// in case the number is negative

if(number < 0) { number = -number; }

int chk = 10;
for(int dgt = 1; dgt < MAXSIZE; dgt++)
{
if(number < chk)
{
return dgt;
}
else
{
chk *= 10;
}
}
//prevent compile warning

return 0;
}


My post up, your post down, my site here


Edited by - Jesper T on February 7, 2002 1:02:19 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
how about using recursion;
[source/]

int n=1;

void count_digit(const int x)
{
if (x < 10) return;
n++;
count_digit(x/10);
}

[/source]

Share this post


Link to post
Share on other sites
Thanks for the replies.

I went ahead and used the method put forward by Jesper. It isn''t the shortest, but it is the closest to the way I see it in my head. It will be easier for me to read in the future.

Thanks again, big help.

Share this post


Link to post
Share on other sites
Costello''s is the best. Just think of it this way: Dividing by ten shifts digits to the right. When a digit goes to the right of the decimal point, it disappears. When the number equals zero, there are no more digits.

Share this post


Link to post
Share on other sites
quote:
Original post by TerranFury
Costello''s is the best. Just think of it this way: Dividing by ten shifts digits to the right. When a digit goes to the right of the decimal point, it disappears. When the number equals zero, there are no more digits.


Not strictly true: it takes a digit to represent 0. That''s why we use the digits 0-9 for the base 10 number system. RobertCostello''s method would be more efficiently implemented as follows:

  
int NumDigitsIn(int num)
{
int digits = 1;
while(num=num/10)
{
digits++;
};

return digits;
}


In base 10, a number is represented by the series

num = a0*100 + a1*101 + a2*102 +...+ an*10n

where the ai are taken from the set {0,1,2,3,4,5,6,7,8,9} and the number of digits is equivalent to the order of the equation (highest power). Taking num div 10 is equivalent to removing the first term from the equation and reducing the power of all other terms by 1.

Starting with digits=1 is necessary because taking the div 10 of a number of order zero (i.e., taking a0/10) will return zero when in fact a0 is a digit. This is why zero also counts as a digit.

Cheers,

Timkin

Share this post


Link to post
Share on other sites