Count the number of digits in C++ code example
Example 1: how to find how many digits a number has in c++
#include <iostream>
#include <cmath>
unsigned int getNumberOfDigits (int i)
{
return i > 0 ? log10((double) i) + 1 : 1;
}
int main()
{
std::cout << "Number of digits: " << getNumberOfDigits(/*Example*/6738) << std::endl;
return 0;
}
Example 2: count digits c++
template <class T>
T countDigits(T number)
{
return T(log10(number) + 1);
}
//If the number is very large, use string