Solved

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

Posted on 2006-10-25
10
316 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[week] - duration other than 7 days? 8 66
Random Variable 2 55
Lock or digital lock that are lockable from both inside plus outside 12 65
Request to review costing formula 3 49
A Guide to the PMT, FV, IPMT and PPMT Functions In MS Excel we have the PMT, FV, IPMT and PPMT functions, which do a fantastic job for interest rate calculations.  But what if you don't have Excel ? This article is for programmers looking to re…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

838 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