C++ - how to find the length of an integer

There is a much better way to do it

    #include<cmath>
    ...
    int size = trunc(log10(num)) + 1
....

works for int and decimal


If you can use C libraries then one method would be to use sprintf, e.g.

#include <cstdio>

char s[32];

int len = sprintf(s, "%d", i);

Not necessarily the most efficient, but one of the shortest and most readable using C++:

std::to_string(num).length()

The number of digits of an integer n in any base is trivially obtained by dividing until you're done:

unsigned int number_of_digits = 0;

do {
     ++number_of_digits; 
     n /= base;
} while (n);