number of diggits in a integer

Started by
30 comments, last by Conner McCloud 17 years, 6 months ago
Dealing with negatives isn't a big deal. just use the absolute value of the number in your check.

#include <iostream>#include <math.h>void main(){	using namespace std;	int number, digits = 1;	cin >>number;		while(abs(number)>=9)	{		number = number/10;		digits++;	}	cout<<digits<<endl;}
Advertisement
num_digits = ceil( log2(maximum_value) / log2(display_radix) );


log2 can be replaced with any other log function (log10 or whatever you have available)
Quote:Original post by EricmBrown
#include <math.h>void main()


#include <cmath>int main()
My basic and simple approach would be like calculating it to a scientific number. that shouldnt be too hard. and how do you get the digits from there??? n * 10^f is your scientific number, numDigits = f+1.


*SPOILER WARNING!!!!*


if you are lazy and would just like the answer it is:
numDigits = (int)log10(abs(num))+1;

that is if the int casting rounds down automatically, if it doesn't im sure you can think of a way to get it to round down.

And for all you people who can't think of this, shame on you, this is super easy

NOTE:if you would like to figure out the number of digits in another base, just do log10(abs(num))/log10(otherBase)+1
Quote:Original post by Nypyren
num_digits = ceil( log2(maximum_value) / log2(display_radix) );

This fails for numbers that are exact powers of 10 (eg. 1000, which reports 3 digits). As suggested above a few times, you need to use floor() + 1, not ceil()

Quote:Original post by Bliz23
that is if the int casting rounds down automatically, if it doesn't im sure you can think of a way to get it to round down.
I don't believe int casting rounds at all, at least not on any platform I've ever used. The typical behavior is to truncate.
#include <math.h>#include <sstream>using namespace std;template<typename T>int numOfDigits(const T &arg){	stringstream ss;	ss << arg;	double test;        ss >> test;	int num = 1;	if(test > 0)	{		num += (int)log10(test);	}	return num;}


Gogogo! you can submit any type and it works! "123.32" = 3, 123.32 = 3, true = 1, "lol" = 1, etc etc,
Quote:Original post by deathkrush
In this case, not only the string-based solution is much slower, the professor probably wouldn't accept it because by using strings you ignore properties of numbers.


In assignments, yes.

In the real world, the primary reason you'd want to know the number of digits in the base-10 representation of a number is that you want to display it (and your display API can only accept a char*). Of course, in those cases, you normally wrap your API so that you can feed it a std::string, and then convert to numbers to strings because that's what you need for your wrapper :\ (Of course, in these cases you don't even really want a *digit* count: you normally want a *character* count, which counts the '-' for a negative number.)

Anyway, the true nature of TDWTFs involving string<->number conversion is usually much more subtle.
One this I don't understand is that if you want to print the value (I'm not sure with C, but don't you use count << number?), somehow the language knows to print the right number of digits, so there must be a way of using the most basic commands to find the answer. Isn't there?
Quote:Original post by Geoff the Medio
Quote:Original post by Nypyren
num_digits = ceil( log2(maximum_value) / log2(display_radix) );

This fails for numbers that are exact powers of 10 (eg. 1000, which reports 3 digits). As suggested above a few times, you need to use floor() + 1, not ceil()


Oi, good call. Fortunately my mistake avoids that case; I use this to determine maximum # of digits for display in a hexeditor to measure width of a textbox, so I use 2^bits-1 as my maximum value, which will never fall on a power of my available radices (2-36, 64).

This topic is closed to new replies.

Advertisement