• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

How do you find the number of digits in a number

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

jlin453

Member
Joined
Sep 18, 2002
Location
Austin, Texas
besides dividing by 10 and keeping a counter? This is in c++.


//ah, int n = log10(n) + 1 works :)
//or ceil(log10(n))
 
Last edited:
You could sprintf it to an array in memory and count the size of the array. You could also use a switch statement to check the size, basically the number of digits is 1 if it's between 0 and 9, 2 if if it's between 10 and 99, etc.) You could also do it with logarithms, but calculating logs is slow, so it's not an efficient way to do it.
 
Back