Jump to content
• Advertisement

#### Archived

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

# counting how many digits in a number?

This topic is 5975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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

##### 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

##### Share on other sites
int digits = strlen(itoa(num,buf,10));

int digits=0;
while(num>0){
num /= 10;
digits++;
}

#### Share this post

##### Share on other sites
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

##### 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

##### 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

##### 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

##### Share on other sites
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

#### Share this post

##### Share on other sites
Thanks for pointing that out Timkin.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement
• ### Popular Tags

• Advertisement

• 18
• 11
• 16
• 9
• 51
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999791
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!