Solved

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

Posted on 2006-10-25
313 Views
Hello. I have another question. How to mathematically find out how many digits a number contain?
0
Question by:m-jansen

LVL 84

Expert Comment

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

LVL 45

Expert Comment

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

LVL 84

Accepted Solution

ozo earned 250 total points
floor(1+log(floor(number)+.9)/log(10))
0

Author Comment

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

LVL 39

Expert Comment

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

LVL 22

Expert Comment

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

LVL 22

Expert Comment

Negative numbers.
Decimals.
0

Author Comment

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

Author Comment

...the number of digits in an VHDL integer
0

Author Comment

I think ozo answer is good enough. Thanks.
0

## Featured Post

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…