# How to mathematically find out how many digits a number contain?

Hello. I have another question. How to mathematically find out how many digits a number contain?
###### Who is Participating?

Commented:
floor(1+log(floor(number)+.9)/log(10))
0

Commented:
floor(log(number)/log(10))
0

Commented:
Hi m-jansen,

Keep dividing the number by increasing powers of 10 until you get quotient 0

div = 10
count =1
while (num/div)
{
div = div * 10
count = count +1
}

Cheers!
sunnycoder
0

Author Commented:
looks like
floor(1+log(number))
gave me the number of digits too... is that right?
0

Commented:
If your log function assumes base 10.  The /log(10) takes care of when log doesn't use base 10.
0

Commented:
I guess it depends how accurate you need the answer to be, and the consequences of a possible off-by-one or two or three or six error.

Also you may not really want this.  For example,  this is the wrong way to calculate how much space you'll need to store the number as text.

If you could explain what you're trying to do maybe we can come up with the best answer.

0

Commented:
Negative numbers.
Decimals.
0

Author Commented:
I am trying to get the length of an integer in VHDL.
0

Author Commented:
...the number of digits in an VHDL integer
0

Author Commented:
I think ozo answer is good enough. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.