Solved

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

Posted on 2006-10-25
10
314 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
ID: 17805796
floor(log(number)/log(10))
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17805805
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
ID: 17805831
floor(1+log(floor(number)+.9)/log(10))
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

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

Expert Comment

by:Adam314
ID: 17806206
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

by:grg99
ID: 17806755
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
ID: 17806885
Negative numbers.
Decimals.
0
 

Author Comment

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

Author Comment

by:m-jansen
ID: 17806943
...the number of digits in an VHDL integer
0
 

Author Comment

by:m-jansen
ID: 17807601
I think ozo answer is good enough. Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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