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 5514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
quote:

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


Nope. log(10) is 2.3. log is the natural logarithm. But I agree this is the best way. Assumes n is an integer and larger than one though.

quote:

floor(log(n)/log(10)) + 1


Or ceil(log(n)/log(10)).

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
Or ceil(log(n)/log(10)).
Unfortunately, ceil(log(1000)/log(10)) == 3.
quote:
Original post by Fruny
And yet, zero has one digit.
Yeah; it would be treated as a special case.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
O.o

ln''s the natural logarithm, isn''t it? log could be any logarithm but when you don''t specify the base it''s presumed to be 10.

Share this post


Link to post
Share on other sites
quote:

Unfortunately, ceil(log(1000)/log(10)) == 3.


True. Didn''t think about the case where log10(n) is an integer. My bad.
quote:

ln''s the natural logarithm, isn''t it? log could be any logarithm but when you don''t specify the base it''s presumed to be 10.


In mathematics, yes. But this is C++, and I suppose the standard says that log should return the natural logarithm.

Share this post


Link to post
Share on other sites