How do I find the ammount of digits in an int
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]
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
hobo-jo
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);
char buf[32];
int i = 523;
int chars_in_i = sprintf("%d", i);
What do you mean there's no size()?
This is the 'C++ way':
std::stringstream::str() returns a std::string& (which is the C++ standard string template)
There's also this more direct C way:
[edited by - Magmai Kai Holmlor on November 9, 2002 1:07:52 AM]
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]
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++.
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.
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.
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 :
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).
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).
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement