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 do I determine the number of digits of an integer in C?
floor (log10 (abs (x))) + 1
http://en.wikipedia.org/wiki/Logarithm
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 to count decimal digits of an integer?
log10
takes various floating point types as arguments and returns the same type in its result. In your case it is converting to double
.
double
only has 53
bits for it's integer (significand) part.
9999999999999999
needs 54 bits to be represented accurately.
When you convert 9999999999999999
to double
you get 10000000000000000
so the result of 16
from log10(9999999999999999)
is to be expected.
To get an accurate (and probably faster) count of digits in an integer you should use an integer method for calculating the digits. There are various techniques for example What is the fastest method of calculating integer Log10 for 64-bit integers on modern x86-64? or http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
Counting number of digits in integer
Inside your while
loop, you are dividing num
by itself, then assigning the result back to num
. This will make num
equal to 1
forever. Change it to
num /= 10;
which will remove the last digit.
Also, you already calculated the last digit and stored the result in outcome
. You might as well use it:
arr[outcome]+=1;
Then, when you get to the loop to print the array, in the condition, always use <
an array length to stop after processing the last index length - 1
. This will prevent the ArrayIndexOutOfBoundsException
that is waiting to show up.
for (int i = 0; i < arr.length; i++){
get digits count in an integer without converting to string
You can use the below :
int count = (int)Math.floor(Math.log10(myInteger) + 1);
PS : It won't work on negative numbers.
Another generic but elongated solution :
int count = 0;
while (yourInteger != 0) {
yourInteger = yourInteger / 10;
++count;
}
return count;
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);
Related Topics
R: Replacing Nas in a Data.Frame with Values in the Same Position in Another Dataframe
R: Replacing Foreign Characters in a String
Weighted Means by Group and Column
How to Create a Hyperlink Interactively in Shiny App
R Shiny - Uioutput Not Rendering Inside Menuitem
Getting the Error "Level Sets of Factors Are Different" When Running a for Loop
Axis Labels for Each Bar and Each Group in Bar Charts with Dodged Groups
Produce a Table Spanning Multiple Pages Using Kable()
Ggplot2: How to Transparently Shade Alternate Days on a Plot
How to Perform a Pairwise T.Test in R Across Multiple Independent Vectors
R Histogram with Multiple Populations
R: Building a Simple Command Line Plotting Tool/Capturing Window Close Events