number of digits in c++
#include <iostream> #include <math.h> using namespace std; int main(){ unsigned long long n = 2345423454234542345 ; // the number n int x = floor(log10(n)) + 1 ; // x = the digit count, cout << x << endl ; // the largest digit can be handled with // is unsigned long long return 0; }
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; }
Source: stackoverflow.com
number of digits in int c++
int length = 1; int x = 234567545; while ( x /= 10 ) length++;
Source: stackoverflow.com
count digits c++
template <class T> T countDigits(T number) { return T(log10(number) + 1); } //If the number is very large, use string