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

后端 未结 13 1387
你的背包
你的背包 2020-12-09 10:03

I\'m trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use

相关标签:
13条回答
  • 2020-12-09 10:18

    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);
    
    0 讨论(0)
  • 2020-12-09 10:18

    There is a much better way to do it

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

    works for int and decimal

    0 讨论(0)
  • 2020-12-09 10:19

    Closed formula for the longest int (I used int here, but works for any signed integral type):

    1 + (int) ceil((8*sizeof(int)-1) * log10(2))
    

    Explanation:

                      sizeof(int)                 // number bytes in int
                    8*sizeof(int)                 // number of binary digits (bits)
                    8*sizeof(int)-1               // discount one bit for the negatives
                   (8*sizeof(int)-1) * log10(2)   // convert to decimal, because:
                                                  // 1 bit == log10(2) decimal digits
        (int) ceil((8*sizeof(int)-1) * log10(2))  // round up to whole digits
    1 + (int) ceil((8*sizeof(int)-1) * log10(2))  // make room for the minus sign
    

    For an int type of 4 bytes, the result is 11. An example of 4 bytes int with 11 decimal digits is: "-2147483648".

    If you want the number of decimal digits of some int value, you can use the following function:

    unsigned base10_size(int value)
    {
        if(value == 0) {
            return 1u;
        }
    
        unsigned ret;
        double dval;
        if(value > 0) {
            ret = 0;
            dval = value;
        } else {
            // Make room for the minus sign, and proceed as if positive.
            ret = 1;
            dval = -double(value);
        }
    
        ret += ceil(log10(dval+1.0));
    
        return ret;
    }
    

    I tested this function for the whole range of int in g++ 9.3.0 for x86-64.

    0 讨论(0)
  • 2020-12-09 10:20

    Most efficient code to find length of a number.. counts zeros as well, note "n" is the number to be given.

    #include <iostream>
    using namespace std;
    int main()
    {
        int n,len= 0;
        cin>>n;
    while(n!=0)
        {
           len++;
           n=n/10;
        }
        cout<<len<<endl;
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-09 10:22

    "I mean the number of digits in an integer, i.e. "123" has a length of 3"

    int i = 123;
    
    // the "length" of 0 is 1:
    int len = 1;
    
    // and for numbers greater than 0:
    if (i > 0) {
        // we count how many times it can be divided by 10:
        // (how many times we can cut off the last digit until we end up with 0)
        for (len = 0; i > 0; len++) {
            i = i / 10;
        }
    }
    
    // and that's our "length":
    std::cout << len;
    

    outputs 3

    0 讨论(0)
  • 2020-12-09 10:23

    Code for finding Length of int and decimal number:

    #include<iostream>
        #include<cmath>
        using namespace std;
        int main()
        {
            int len,num;
            cin >> num;
            len = log10(num) + 1;
            cout << len << endl;
            return 0;
        }
        //sample input output
        /*45566
        5
    
        Process returned 0 (0x0)   execution time : 3.292 s
        Press any key to continue.
        */
    
    0 讨论(0)
提交回复
热议问题