How do I find the ammount of digits in an int

Started by
12 comments, last by Brother Bob 21 years, 5 months ago
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]
Advertisement
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
love the big green hobo
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);
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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++.
floor(log(n)/log(10)) + 1, or something.
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.
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).
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.

Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote:
floor(log(n)/log(10))+1


limx→0+ log(x)=-∞

And yet, zero has one digit.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on November 9, 2002 11:19:10 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement