C++ - how to find the length of an integer

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C++ - how to find the length of an integer



I'm trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use of classes from the STL, although the program spec does say we can use "common C libraries" (gonna ask my professor if I can use cmath, because I'm assuming log10(num) + 1 is the easiest way, but I was wondering if there was another way).



Ah, and this doesn't have to handle negative numbers. Solely non-negative numbers.



I'm attempting to create a variant "MyInt" class that can handle a wider range of values using a dynamic array. Any tips would be appreciated! Thanks!





What is the "length of an integer"? What's the colour of a happy thought?
– Kerrek SB
Mar 26 '14 at 0:14





What do you define as the length of an integer?
– Ben
Mar 26 '14 at 0:15





Ah, sorry. To clarify, I mean the number of digits in an integer, i.e. "123" has a length of 3.
– user1888527
Mar 26 '14 at 0:15





@KerrekSB Actually I think he means the total unsigned value. That would be 2 to the power of ( sizeof( int ) * 8 - 1 ), right? EDIT: Nevermind.
– Ben
Mar 26 '14 at 0:16






The number of digits in a decimal integer is (log(number) / log(10)).
– Thomas Matthews
Mar 26 '14 at 0:16




13 Answers
13



The number of digits of an integer n in any base is trivially obtained by dividing until you're done:


n


unsigned int number_of_digits = 0;

do
++number_of_digits;
n /= base;
while (n);



Not necessarily the most efficient, but one of the shortest and most readable using C++:


std::to_string(num).length()





Performance vs the KerrekSB's answer?
– Davdriver
Dec 27 '17 at 13:04





Wouldn't be correct for negative numbers.
– Daniel Ryan
Feb 27 at 2:47





@Zammbi the question literally states "this doesn't have to handle negative numbers".
– Riot
Feb 27 at 9:07



If you can use C libraries then one method would be to use sprintf, e.g.


#include <cstdio>

char s[32];

int len = sprintf(s, "%d", i);





You could use snprintf(nullptr, 0, "%d", i);` and not need s.
– Tony Delroy
Mar 26 '14 at 0:37



s



"I mean the number of digits in an integer, i.e. "123" has a length of 3"


int i = 123;

// the "length" of 0 is 1:
int len = 1;

// and for numbers greater than 0:
if (i > 0)
// we count how many times it can be divided by 10:
// (how many times we can cut off the last digit until we end up with 0)
for (len = 0; i > 0; len++)
i = i / 10;



// and that's our "length":
std::cout << len;



outputs 3


3





But 0 has one digit.
– Paul R
Mar 26 '14 at 0:20





@PaulR: Fixed :)
– LihO
Mar 26 '14 at 0:25



Closed formula for the size of the int:


int


ceil(8*sizeof(int) * log10(2))



EDIT:



For the number of decimal digits of some value:


ceil(log10(var+1))



This works for numbers > 0. Zero must be checked separately.


> 0





it does not even use a variable name...
– test30
Jan 22 '16 at 15:21





This will give the maximum number of decimal digits an int can hold.
– lvella
Jan 22 '16 at 16:26


int





ceil(log10(var+1)) will fail for INT_MAX
– bricklore
Jul 28 '16 at 13:38


ceil(log10(var+1))





This is not reliable, the max value of type will fail. Totally agree with bricklore. I prefer std::to_string(num).length() as Riot said.
– 123iamking
Aug 21 '17 at 7:07



int intLength(int i)
int l=0;
for(;i;i/=10) l++;
return l==0 ? 1 : l;



Here's a tiny efficient one





you have to save the case i == 0. which is also 1 and current function returns 0;
– sergio
Feb 7 '16 at 22:58





thanks, now it should be ok
– Naheel
Feb 8 '16 at 20:56



Being a computer nerd and not a maths nerd I'd do:


char buffer[64];
int len = sprintf(buffer, "%d", theNum);



There is a much better way to do it


#include<cmath>
...
int size = log10(num) + 1
....



works for int and decimal



How about (works also for 0 and negatives):


int digits( int x )
return ( (bool) x * (int) log10( abs( x ) ) + 1 );



Would this be an efficient approach? Converting to a string and finding the length property?


int num = 123
string strNum = to_string(num); // 123 becomes "123"
int length = strNum.length(); // length = 3
char array[3]; // or whatever you want to do with the length





Not very efficient though.
– Rafael Campos Nunes
Apr 21 '17 at 0:19



Code for finding Length of int and decimal number:


#include<iostream>
#include<cmath>
using namespace std;
int main()

int len,num;
cin >> num;
len = log10(num) + 1;
cout << len << endl;
return 0;

//sample input output
/*45566
5

Process returned 0 (0x0) execution time : 3.292 s
Press any key to continue.
*/



Best way is to find using log, it works always


int len = ceil(log10(num))+1;



Plain and pretty simple function


int intlen(int i)

int j=1;
while(i>10)i=i/10;j++;
return j;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard