Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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?
0
m-jansen
Asked:
m-jansen
1 Solution
 
ozoCommented:
floor(log(number)/log(10))
0
 
sunnycoderCommented:
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
 
ozoCommented:
floor(1+log(floor(number)+.9)/log(10))
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
m-jansenAuthor Commented:
looks like
floor(1+log(number))
gave me the number of digits too... is that right?
0
 
Adam314Commented:
If your log function assumes base 10.  The /log(10) takes care of when log doesn't use base 10.
0
 
grg99Commented:
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
 
NovaDenizenCommented:
Negative numbers.
Decimals.
0
 
m-jansenAuthor Commented:
I am trying to get the length of an integer in VHDL.
0
 
m-jansenAuthor Commented:
...the number of digits in an VHDL integer
0
 
m-jansenAuthor Commented:
I think ozo answer is good enough. Thanks.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now