I have an integer and need to find out how many digits are in it.
A little tricky to handle negative numbers and the case where the input is zero:
int length(int n)
{
int len = 0;
if (n < 0) { len = 1; n = -n; }
while (n > 9) {
n /= 10;
len++;
}
return len+1;
}
There is no such function available in the C++ library. However you can use std::stringstream for simplicity.
Try this (Handles negative numbers as well).
int a =-12345,x;
x = std::abs(a)
std::stringstream s;
s << x;
std::cout<<s.str().size();
You have to keep dividing it by 10 (assuming it is an integer). You do this because you remove a digit each time the loop iterates.
something along the lines of:
int number;
int digits;
while (number > 0)
{
digits++;
number /= 10;
}
You'll probably want to make sure the number is not zero to begin with.
int intlen(float num) {
int cnt = 0;
while(num >= 1) {
num = num / 10;
cnt++;
}
return cnt;
}
For positive numbers, use log10:
int a = 1234;
int len = static_cast<int>(log10(a)+1.);
If you need to be thorough:
int length(int a)
{
int b = abs(a);
if (b == 0) return 1;
return static_cast<int>(log10(b)+1.);
}
With that said, it would be a better choice to do repeated division by 10 in practice.
int length(int a)
{
int b = 0;
for (a = abs(a); a != 0; b++, a /= 10) continue;
return b;
}
You probably mean you have a string containing numbers rather than an int in python:
>>> i = 123456789
>>> len(i)
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>> len(str(i))
9
If this is also the case in c++ it's easy to find the length of a string using:
my_str_value.length()
or for a C string using strlen
Here is a little example:
int numberDigits(int n) {
char buffer[100];
itoa(n,buffer,10);
int len=0;
while (buffer[len]!=0) { len++; }
return len;
}
Hmm... Python:
>>> len(5)
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
len(5)
TypeError: object of type 'int' has no len()
not what you wanted?
well, lets suppose you have an actual integer. the log base 10 will tell you what you want to know numerically, that is if yournumber == pow(10, digits), then log10(yournumber) == digits! unfortunately, if your number is not an exact power of 10, you will have a fraction part to deal with. That's easy enough to deal with, though, with the floor() function, which just rounds down. be wary of negative numbers, as logarithms are undefined in the real numbers for non-positive values.
#include <iostream>
#include <math.h>
int main()
{
std::cout << floor(log10(5))+1 << std::endl;
std::cout << floor(log10(30))+1 << std::endl;
std::cout << floor(log10(2000))+1 << std::endl;
std::cout << floor(log10(16000))+1 << std::endl;
}
we have to add 1 because 10 to the 1'st is still 10, so we're off by one. Add one to the exponent and you have digits!
Ignoring for the moment that len() in python returns the number of items in a sequence and not the number of digits in an integer, here's a function to count the number of digits in an integer without dividing (so it should be much faster than the similar solutions which use division).
int number_of_digits(int value)
{
int count = 0;
int i = 1;
if (value < 0)
{
value *= -1;
}
while (i < value)
{
count++;
i *= 10;
}
if (count > 0)
{
return count;
}
else
{
return 1;
}
}
For extra speed, you can even replace the multiplication by ten with some bit twiddling:
i = ((i << 2) + i) << 1;
(The bit shifting is cool, but the multiplication may be "free" if your CPU can pipeline the multiplication on some otherwise unused multiplication unit - modern processors are a thing of beauty).