I have a number like this: int num = 36729;
and I want to get the number of digits that compose the number (in this case 5 digits).
How can I do this?>
unsigned int number_of_digits = 0;
do {
++number_of_digits;
n /= base;
} while (n);
Not necessarily the most efficient, but one of the shortest and most readable using C++:
std::to_string(num).length()
And there is a much better way to do it:
#include
...
int size = trunc(log10(num)) + 1
...