Way to get number of digits in an int?
Your String-based solution is perfectly OK, there is nothing "un-neat" about it. You have to realize that mathematically, numbers don't have a length, nor do they have digits. Length and digits are both properties of a physical representation of a number in a specific base, i.e. a String.
A logarithm-based solution does (some of) the same things the String-based one does internally, and probably does so (insignificantly) faster because it only produces the length and ignores the digits. But I wouldn't actually consider it clearer in intent - and that's the most important factor.
How to find length of digits in an integer?
If you want the length of an integer as in the number of digits in the integer, you can always convert it to string like str(133)
and find its length like len(str(123))
.
Finding the number of digits of an integer
There's always this method:
n = 1;
if ( i >= 100000000 ) { n += 8; i /= 100000000; }
if ( i >= 10000 ) { n += 4; i /= 10000; }
if ( i >= 100 ) { n += 2; i /= 100; }
if ( i >= 10 ) { n += 1; }
How do I determine the number of digits of an integer in C?
floor (log10 (abs (x))) + 1
http://en.wikipedia.org/wiki/Logarithm
Finding the length of an integer in C
C:
Why not just take the base-10 log of the absolute value of the number, round it down, and add one? This works for positive and negative numbers that aren't 0, and avoids having to use any string conversion functions.
The log10
, abs
, and floor
functions are provided by math.h
. For example:
int nDigits = floor(log10(abs(the_integer))) + 1;
You should wrap this in a clause ensuring that the_integer != 0
, since log10(0)
returns -HUGE_VAL
according to man 3 log
.
Additionally, you may want to add one to the final result if the input is negative, if you're interested in the length of the number including its negative sign.
Java:
int nDigits = Math.floor(Math.log10(Math.abs(the_integer))) + 1;
N.B. The floating-point nature of the calculations involved in this method may cause it to be slower than a more direct approach. See the comments for Kangkan's answer for some discussion of efficiency.
C++ - how to find the length of an integer
The number of digits of an integer n
in any base is trivially obtained by dividing until you're done:
unsigned int number_of_digits = 0;
do {
++number_of_digits;
n /= base;
} while (n);
How can I get a count of the total number of digits in a number?
Without converting to a string you could try
Math.Floor(Math.Log10(n) + 1);
How can I count the digits in an integer without a string cast?
This should do it:
int length = (number ==0) ? 1 : (int)Math.log10(number) + 1;
PHP - Get length of digits in a number
Maybe:
$num = 245354;
$numlength = strlen((string)$num);
Related Topics
How to Load All Entries in an Infinite Scroll at Once to Parse the HTML in Python
How to Find Children of Nodes Using Beautifulsoup
Does Anybody Know How to Identify Shadow Dom Web Elements Using Selenium Webdriver
Convert HTML Entities to Unicode and Vice Versa
Python Multiprocessing Linux Windows Difference
Performance of Pandas Apply VS Np.Vectorize to Create New Column from Existing Columns
How to Access the Query String in Flask Routes
How to Validate a Url with a Regular Expression in Python
How to Specify New Lines on Python, When Writing on Files
How to Validate Ip Address in Python
How to Run an External Command Asynchronously from Python
Is Shared Readonly Data Copied to Different Processes for Multiprocessing
In Python, How to Read the Exif Data for an Image
In Python, How to Convert a 'Datetime' Object to Seconds
Why Is Dictionary Ordering Non-Deterministic
How to Make a Single Legend for Many Subplots with Matplotlib