Solved

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

Posted on 2006-10-25
10
313 Views
Last Modified: 2008-02-01
Hello. I have another question. How to mathematically find out how many digits a number contain?
0
Comment
Question by:m-jansen
10 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
floor(log(number)/log(10))
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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

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

Author Comment

by:m-jansen
Comment Utility
looks like
floor(1+log(number))
gave me the number of digits too... is that right?
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
If your log function assumes base 10.  The /log(10) takes care of when log doesn't use base 10.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:grg99
Comment Utility
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

by:NovaDenizen
Comment Utility
Negative numbers.
Decimals.
0
 

Author Comment

by:m-jansen
Comment Utility
I am trying to get the length of an integer in VHDL.
0
 

Author Comment

by:m-jansen
Comment Utility
...the number of digits in an VHDL integer
0
 

Author Comment

by:m-jansen
Comment Utility
I think ozo answer is good enough. Thanks.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now