# number of diggits in a integer

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I find the number of digits in a integer?? like: 1 = 1 10 = 2 100 = 3 I tried with i.size() but that didnt work.. :/

##### Share on other sites
What language are you using?

c++

##### Share on other sites
for(int i = 0; x > 9; x /= 10, i++);

Would that work?

##### Share on other sites
Quote:
 Original post by Nice Coderfor(int i = 0; x > 9; x /= 10, i++);Would that work?

i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me

##### Share on other sites
Depends on how the number is represented. Decimal,hex,bin,octal or others system could produce different numbers of digits.

Lets take the number 123d in decimal:

-123d in decimal(123) and has 3 digits,
-123d in hex(7Bh) has 2 digits,
-123d in binary(1111011b) has 7 digits
-123d in octal(173o) has 3 digits.

##### Share on other sites
Quote:
 Original post by Palidinei will be off by one and not accesible outside the scope of the for loop.

Not to mention that it's horribly inefficient to use a loop to do something for which a simple formula exists.

##### Share on other sites
The best way to do this is to convert the number to a string and find the length of the string.

(just kidding)

##### Share on other sites
Quote:
 Original post by deathkrushThe best way to do this is to convert the number to a string and find the length of the string. (just kidding)

hum.. why not?

##### Share on other sites
Quote:
 Original post by deathkrushThe best way to do this is to convert the number to a string and find the length of the string. (just kidding)

:)
Ever read The Daily WTF? It's amazing how often the "convert to string" approach is taken in real-world applications by programmers who should know better.

##### Share on other sites
Quote:
 Original post by PlasmadogEver read The Daily WTF? It's amazing how often the "convert to string" approach is taken in real-world applications by programmers who should know better.

I was just going to say that. It's funny how the first solution that comes to mind is usually a WTF one.

##### Share on other sites
but seriously.. im a newb.. why? :D

##### Share on other sites
Quote:
 Original post by Nanookbut seriously.. im a newb.. why? :D

Because the problem you are trying to solve is just basic maths. There is a mathematical solution.

##### Share on other sites
Quote:
 Original post by Nanookbut seriously.. im a newb.. why? :D

Because it's equivalent to printing out the number, putting the print out on a wooden table, taking a picture, scanning the picture and then using an image recognition program to find the number of digits. :-)

Seriously, though, if there is a simple formula to calculate the number of digits, there is no need to get wooden tables involved. 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. Knowing these properties will help you in programming.

##### Share on other sites
You could cast the integer (344) to double (344.0), use log10() to get the base-10 logarithm (2.536), cast back to integer (2) and add 1 (3).

Edit: but have a special case check for 0

##### Share on other sites
Quote:
Original post by Palidine
Quote:
 Original post by Nice Coderfor(int i = 0; x > 9; x /= 10, i++);Would that work?

i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me

Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?

##### Share on other sites
Alternatively, you can do:

int numberOfDigits = 1;
if(theNumber > 0)
numberOfDigits += (int)log(myNumber);
};

This will give number of digits. :)

##### Share on other sites
Quote:
Original post by superdeveloper
Quote:
Original post by Palidine
Quote:
 Original post by Nice Coderfor(int i = 0; x > 9; x /= 10, i++);Would that work?

i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me

Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?

Only if you are using VC++ 6.

##### Share on other sites
Quote:
 Original post by superdeveloper...and it IS accessible outside the scope of the loop. Why do you think it's not?

It won't be, if the compiler is fully standards-compliant. It shouldn't be relied upon.

##### Share on other sites
I don't see where the i fits into that solution . . . except as a counter, but still. My solution is nearly the same, just slightly different.

digits = 0
for(count = 10; count<goal_num; count *= 10, digits++);

When count is greater than the goal number it'll stop, so digits will equal the number of digits. The only problem, I guess, is for negative numbers.

##### Share on other sites
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;}

##### Share on other sites
num_digits = ceil( log2(maximum_value) / log2(display_radix) );

log2 can be replaced with any other log function (log10 or whatever you have available)

##### Share on other sites
Quote:
 Original post by EricmBrown#include void main()

#include <cmath>int main()

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Nypyrennum_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()

##### Share on other sites

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628718
• Total Posts
2984374

• 25
• 11
• 10
• 14
• 14