0

I need to store the number of digits from a number into a variable. e.g. 538, i need to store 3. How can I accomplish this is C++? Thanks.

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by Moschops
1

Here is something to start with:

int getNrDigits(int my_int)
{
    int local_int = my_int;
    int count = 0;

    while (local_int > 0)
    {
        local_int /= 10;
        count++;
    }

    return count;
}

I hope I didn't missed anything because this was programming on paper at late hour as somebody called it once. :)

0

It is interesting to note the relationship between log(base 10) of an integer and the number of digits in that integer.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.