Advertisement

counting how many digits in a number?

Started by February 07, 2002 11:15 AM
10 comments, last by leggyguy 23 years ago
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.)
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"<}
Advertisement
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
int digits = strlen(itoa(num,buf,10));

int digits=0;
while(num>0){
num /= 10;
digits++;
}
--Robert Costellowwww.playfulminds.com
how about using recursion;
[source/]

int n=1;

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

[/source]
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.
Advertisement
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.
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
Maybe I''m wrong but I think you can use logarithm (as compr0 said):

log(n)=ln(n)/ln(10)=num_digits

where num_digits is rounded to the next integer

for ex : log(4545115)=6.6575... => rounded to 7 => 7 digits
I know that I don't know nothing... Operation Ivy
Thanks for pointing that out Timkin.

This topic is closed to new replies.

Advertisement