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

I am trying to find a way to find the length of an integer (the number of digits), and then put it in an integer array. The assignment also requires you to do this without using classes from STL, although the program specification says that we can use the “common C libraries” (ask my professor if I can use cmath because I assume log10 (num) + 1 is the easiest way, but I was wondering if there is another way).

Ah, and this should not handle negative numbers. Extremely non-negative numbers.

I am trying to create a variant of "MyInt" that can handle a wider range of values ​​using a dynamic array. Any advice would be appreciated! Thanks!

+14
c ++ integer numbers count digits
source share
14 answers

The number of digits of the integer n in any base is trivially obtained by dividing until you finish:

 unsigned int number_of_digits = 0; do { ++number_of_digits; n /= base; } while (n); 
+23
source share

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

 std::to_string(num).length() 
+11
source share

If you can use the C libraries, then one of the methods would be to use sprintf , for example

 #include <cstdio> char s[32]; int len = sprintf(s, "%d", i); 
+10
source share

"I mean the number of digits in integers, that is," 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 our "length": std::cout << len; 

outputs 3

+6
source share

Closed formula for int size:

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

EDIT:

The number of decimal digits of some value:

 ceil(log10(var+1)) 

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

+4
source share
 int intLength(int i) { int l=0; for(;i;i/=10) l++; return l==0 ? 1 : l; } 

Here tiny effective

+4
source share

There is a much better way to do this.

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

works for int and decimal

+3
source share

Being a computer nerd, not a mathematician, I would do:

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

How about (also works for 0 and negatives):

 int digits( int x ) { return ( (bool) x * (int) log10( abs( x ) ) + 1 ); } 
+2
source share

It’s best to find using a magazine, it always works

 int len = ceil(log10(num))+1; 
+2
source share

Would this be an effective approach? Convert to string and search for 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 
+1
source share

Code for finding the length of an integer 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. */ 
+1
source share

Simple and fairly simple function

 int intlen(int i) { int j=1; while(i>10){i=i/10;j++;} return j; } 
-one
source share

The most efficient code for determining the length of a number ... also takes into account zeros, note that "n" is the number to be given.

 #include <iostream> using namespace std; int main() { int n,len= 0; cin>>n; while(n!=0) { len++; n=n/10; } cout<<len<<endl; return 0; } 
-one
source share

All Articles