# Thread: number of digits in number?

1. ## number of digits in number?

Is there any function that calculates the number of digits in an int?

2. The best way i can think of doing this, is to convert the int to a string using itoa(), then use a while loop with a condition to break when the first '\0' is encountered. Therefore, the string will have to be null terminated before you use itoa, within the while loop you would have a counter incrementing until the condition becomes false. You then have the number of digits from the int.

3. Code:
```#include <iostream.h>

int main(void)
{
int Num, Digit=0;
static char *StringNum;

cout << "Please enter number: ";
cin >> Num;

StringNum = new char[sizeof Num + 1]; //'\0 +1'

itoa(Num, StringNum, 10);

while(StringNum[Digit] != '\0')
Digit++;

delete StringNum;
StringNum=NULL;

cout << "\nThe number: " << Num << ". Contains " << Digit << " digit(s).";
getchar();

return 0;
}```

4. Peform a loop that increments a counter variable and divide the number or a copy of the number by the base until it reaches 0.
Code:
```#include <iostream.h>

int main ( )
{
int number = 64789, copyof;
int count = 0;

copyof = number;
while (copyof){
count++;
copyof /= 10;
}
cout<<"There are "<<count<<" digits in "<<number<<".\n";

return 0;
}```

5. I got a little bored. Looks like division by 10 is the way to go.

Code:
```#include <cmath>
#include <ctime>
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

const int test_iterations=int(1e7);

int numDigitsWithDivBy10(int n) {
if (n == 0) return 1;
int count=0;
while (n) {
count++;
n/=10;
}
return count;
}

int numDigitsWithLog(int n) {
if (n < 0) n = -n;  // log of negative illegal
else if (n == 0) return 1; // log 0 illegal
return int(log10(double(n)))+1;
}

int numDigitsWithSStream(int n) {
if (n < 0) n = -n; // don't want to count negative sign in length of string
ostringstream out;
out << n;
return (int) out.str().length();
}

// test for correct results
void test(int n) {
std::cout << setw(13) <<  n << '\t' << numDigitsWithLog(n) << '\t' << numDigitsWithSStream(n)
<< '\t' << numDigitsWithDivBy10(n) << endl;
}

int main() {
int i;

for (i = 1; i < INT_MAX/10; i*=10) {
test(i);
test(i - 1);
test(-i);
test(-i + 1);
}

time_t start=time(NULL);
for (i = 0; i < test_iterations; ++i) {
numDigitsWithLog(i);
}
cout << "numDigitsWithLog for " << test_iterations << " took " << time(NULL) - start << " seconds " <<endl;

/*	start=time(NULL);
for (i = 0; i < test_iterations; ++i) {
numDigitsWithSStream(i);
}
cout << "numDigitsWithSStream for " << test_iterations << " took " << time(NULL) - start << " seconds " <<endl;
*/

start=time(NULL);
for (i = 0; i < test_iterations; ++i) {
numDigitsWithDivBy10(i);
}
cout << "numDigitsWithDivBy10 for " << test_iterations << " took " << time(NULL) - start << " seconds " <<endl;

return 0;
}```

6. Forgot to log in, so I can't edit it . Oh well.

7. Originally posted by silentstrike
I got a little bored.
This is what you do when you're bored?

8. Yeah.. pretty enjoyable

9. what is sstream header in silentstrike's code?

10. It for NumDigitsWithSStream. It basically lets you write to a string with <<. It similair to cout (in that it is an output stream), but instead of writing to the console, it writes to it's own private buffer, which can be converted to a string with the .str() method.

11. or you can use log10().
Here's an example

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int n=1078;
cout<<n<<" has "<<static_cast<int>(log10(n))+1<<" digits\n";
return 0;
}

12. I had that in my post already.. benchmarked it, ran about 2.5 times slower than the division by 10 method.

13. Well if you're out for speed, try this.

It's a bit long compared to some of the others, but it is rather quick

Would be somewhat unpleasant to extent to 64 bits though

Code:
```int num_len ( int n ) {
if (n < 0) n = -n;
if ( n <= 99999 ) {
if ( n <= 999 ) {
if ( n <= 9 )  return 1;
if ( n <= 99 ) return 2;
return 3;
} else {
if ( n <= 9999 ) return 4;
else             return 5;
}
} else {
if ( n <= 9999999 ) {
if ( n <= 999999 ) return 6;
else               return 7;
} else {
if ( n <= 99999999 ) return 8;
else                 return 9;
}
}
}```
The stats for 1e8 iterations (too quick to measure at 1e7)
numDigitsWithLog for 100000000 took 67 seconds
numDigitsWithDivBy10 for 100000000 took 31 seconds
num_len for 100000000 took 3 seconds

Code:
```int num_digits(int n)
{
int count = 0;

if (n < 10)
return 1;
else if (n < 100)
return 2;

do {
count += 3;
n /= 1000;
} while(n >= 1000);

if (n) {
count++;
if (n >= 10) {
count++;
if (n >= 100)
count++;
}
}

return count;
}```

15. Still somewhat slower. You can cut out the top if statements
and make the do while into a while.