counting how many digits in a number?
I am currently using something like
int a, digits;
if (a < 10) digits = 1;
else if (a < 100) digits = 2;
etc etc upto 10 digits.
There must be a tidier way. (I am using this so that the figures are printed in a certain place depending on amount of digits in the number.)
using properties of log you can accomplish this
#include <math.h>
int digitsInNumber(int _in)
{
return (int(log(_in)/log(10))+1);
}
int main()
{
int number;
cout << "enter the number" << endl;
cin >> number;
cout << "that number has " << digitsInNumber(number) << " digits"<}
#include <math.h>
int digitsInNumber(int _in)
{
return (int(log(_in)/log(10))+1);
}
int main()
{
int number;
cout << "enter the number" << endl;
cin >> number;
cout << "that number has " << digitsInNumber(number) << " digits"<
Well if you consider the way the log funcion really looks its still untidy (and a lot slower too I guess)
edit:
I'd use something like this:
My post up, your post down, my site here
Edited by - Jesper T on February 7, 2002 1:02:19 PM
edit:
I'd use something like this:
|
Edited by - Jesper T on February 7, 2002 1:02:19 PM
int digits = strlen(itoa(num,buf,10));
int digits=0;
while(num>0){
num /= 10;
digits++;
}
int digits=0;
while(num>0){
num /= 10;
digits++;
}
--Robert Costellowwww.playfulminds.com
February 07, 2002 12:07 PM
how about using recursion;
[source/]
int n=1;
void count_digit(const int x)
{
if (x < 10) return;
n++;
count_digit(x/10);
}
[/source]
[source/]
int n=1;
void count_digit(const int x)
{
if (x < 10) return;
n++;
count_digit(x/10);
}
[/source]
Thanks for the replies.
I went ahead and used the method put forward by Jesper. It isn''t the shortest, but it is the closest to the way I see it in my head. It will be easier for me to read in the future.
Thanks again, big help.
I went ahead and used the method put forward by Jesper. It isn''t the shortest, but it is the closest to the way I see it in my head. It will be easier for me to read in the future.
Thanks again, big help.
Costello''s is the best. Just think of it this way: Dividing by ten shifts digits to the right. When a digit goes to the right of the decimal point, it disappears. When the number equals zero, there are no more digits.
quote:
Original post by TerranFury
Costello''s is the best. Just think of it this way: Dividing by ten shifts digits to the right. When a digit goes to the right of the decimal point, it disappears. When the number equals zero, there are no more digits.
Not strictly true: it takes a digit to represent 0. That''s why we use the digits 0-9 for the base 10 number system. RobertCostello''s method would be more efficiently implemented as follows:
|
In base 10, a number is represented by the series
num = a0*100 + a1*101 + a2*102 +...+ an*10n
where the ai are taken from the set {0,1,2,3,4,5,6,7,8,9} and the number of digits is equivalent to the order of the equation (highest power). Taking num div 10 is equivalent to removing the first term from the equation and reducing the power of all other terms by 1.
Starting with digits=1 is necessary because taking the div 10 of a number of order zero (i.e., taking a0/10) will return zero when in fact a0 is a digit. This is why zero also counts as a digit.
Cheers,
Timkin
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement