Search code examples
c++doubledigits

C++ count the number of digits of a double


i want to do what the title says like this:

int number1;
cin>>number1;
num1len=log10(number1)+1;
cout<<"num of digits is "<<num1len<<"\n";

but when the number of digits is 11 and more the answer is always 7(6+1)

Does anyone knows why or what im i doing wrong?


Solution

  • What is 'wrong' is the maximum value which can be stored in a (signed) int :

    #include <iostream>
    #include <numeric>
    
    int main()
    {
        std::cout << std::numeric_limits<int>::max() << std::endl;
    }
    

    Gives me :

    2147483647