Public Group

#### Archived

This topic is now archived and is closed to further replies.

# How do I find the ammount of digits in an int

This topic is 5734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to find out ahway of telling how many digits are in an int in C++. For example if I had int i = 534; I would need to know there are 3 digits. I'm looking for something like string.size(); only for a integer. Any help is greatly appreciated! [edited by - Shenron on November 8, 2002 12:34:18 AM]

##### Share on other sites
Im just learning c++ atm, and we just covered something similar to this in class. as far as I know there is no size() in c++. We had to use modulus (%) with a while loop, and have a counter count the number of times a number is taken off the end using modulus or something along those lines. Im sure a more experienced user will be able to clear up the exact way to do this soon.

hobo-jo

##### Share on other sites
Convert it to a string, run it through a really big switch statement, or take the upper bound of log10.

char buf[32];
int i = 523;
int chars_in_i = sprintf("%d", i);

##### Share on other sites
What do you mean there's no size()?

This is the 'C++ way':

        #include <sstream> int chars_in_an_int(int i){using namespace std;stringstream ss;ss << i;return ss.str().size();}

std::stringstream::str() returns a std::string& (which is the C++ standard string template)

There's also this more direct C way:
char buf[12];int n = strlen( itoa(534, buf, 10) );

[edited by - Magmai Kai Holmlor on November 9, 2002 1:07:52 AM]

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor
This is the ''C++ way'':
<snip>

it''s code like this that makes me very, very afraid of c++.

##### Share on other sites
floor(log(n)/log(10)) + 1, or something.

##### Share on other sites
Try dividing by 1, and if it isn''t 0, multiply the 1 by 10 and divide again. Keep doing this until you get 0 (assuming you are using ints). The number of times that you can do this and not get zero, is the number of digits (including 0).

123/1=123
123/10=12;
123/100=1;
123/100=0;
3 digits

10/1=10;
10/10=1;
10/100=0;
2 digits

It isn''t the best way, but possibly the easiest to understand.

##### Share on other sites
Note that the string method count the minus sign whereas this one doesn''t.

This should be slighty faster than converting it to a string :

int getNbDigits( int n){    int i = 0;    do{         n /= 10;         i++;    while ( n > 0);    return i;}

PS : if you deal with really big numbers you should perhaps use BeerHunter''s methode since it runs always as fast, no matter the integer size, though on small numbers it may be slower (I didn''t test it).

##### Share on other sites
quote:
Original post by Beer Hunter
floor(log(n)/log(10)) + 1, or something.

I recommend this way, although log(10) is 1, so you can safely ignore the dividing part.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×