954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

count digits

Hi,

wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4

savinki
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

You'll need to use the modulo (or remainder) operator: %

It does what it says: cout << (123 % 10);

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Hi,

wt is the function that can use to count number of digits in a long integer? e.g. 12345678 -> 8 11 ->2 456 -> 3 6724 -> 4

I don't know that there is a function that does that. You can do two things though. You can use the log10 function from cmath: http://www.cplusplus.com/reference/clibrary/cmath/log10.html

That function takes and returns doubles, so you'll need to do some typecasting and adjust by one.

Or you can set up a for-loop and divide by 10 each time through the loop using integer division. Count how many times you go through the loop.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

or....

int getNumDecimalDigits(int value)
{
    char myString[16];
    sprintf(myString,"%d",value);
    return strlen(myString);
}

:P


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Hi,

wt is the function that can use to count number of digits in a long integer? e.g. 12345678 -> 8 11 ->2 456 -> 3 6724 -> 4


Hey Saviniki try this code:
#include
void main()
{
long int a;
int count=0,;
printf("Enter a interger :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
count++;
}
printf("No. of digits in the no entered : %d",count);
}

deepak1331
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

@Deepak
Do not give away the solution. Read the last point of this link http://www.daniweb.com/forums/thread78223.html

Also posting C code in C++ forum is not a good idea that too without code tags.

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 
int getNumDecimalDigits(int value) { char myString[16]; sprintf(myString,"%d",value); return strlen(myString); }

That is not the best way to do it, heres a better way:

// Will work with negative numbers
int CountDidgets(int val) {
	int d = 1, c;
	if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
	else for (c = -10 ; c >= val; c *= 10) d++;
	return (c < 0) ? ++d : d;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

> That is not the best way to do it
There's no such thing as the best overall way to do it. Your way might be better in Edward's program but worse in jephthah's, or vice versa. Though jephthah's function has the benefit of simplicity. :)

> heres a better way
The results are identical for the most part. Ed would argue that the sign has no business being counted as a digit, but that's something that depends on the needs of the program as well. ;) One big problem with your better way is that it fails to handle INT_MIN and INT_MAX.

Edward would do something more like this for the counting method:

int CountDigits(int value)
{
  int count = 0;

  value = std::abs(value);

  do {
    ++count;
    value /= 10;
  } while (value != 0);

  return count;
}
Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 
The results are identical


Not exactly, the function I made isMuch more efficient and doesnt make any calls to any other functions. And I think the minus sign should count as a didget as if you are don't, trying to assign it to a char buffer and it wont have enough space if it's a negative number.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

> the function I made is Much more efficient
Edward would argue that an infinite loop for some inputs isn't much more efficient. ;) Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function. Finally, pushing for efficiency at the cost of clarity without bottleneck benchmarks to back up the decision is usually a bad idea.

> trying to assign it to a char buffer and it wont have enough space if it's a negative number
That makes sense if all you use this function for is allocating buffers. There are other uses as well, and counting the sign would be treated as an error in many of them. I think you should avoid counting the sign and let the caller decide what to do:

int n = CountDigits(value);

if (value < 0 || includeSign)
  ++n;

...

But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:

int n = CountDigits(value);

if (value < 0)
  --n;

...
Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

Ive had that function for a year or two now, never failed on me o.0

Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function.


I agree there, although I would rather have both.
But I dont see how its broken.. :icon_confused:

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

You could also change the function very easily so you have a choise:

int CountDidgets(int val, bool signInc = 0) {
	int d = 1, c;
	if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
	else for (c= -10 ; c >= val; c *= 10) d++;
	return (signInc && c < 0) ? ++d : d;
}

Now were both happy :)

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

best shmest.

I'm not trying to give bulletproof homework solutions.

i was just pointing out how one could "count characters" just as easily as one could divide/modulo


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
But I dont see how its broken.. :icon_confused:

Edward made a good point there, if you try

cout << CountDidget(INT_MAX) << '\n';


What happens :?:

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Edward made a good point there, if you try

cout << CountDidget(INT_MAX) << '\n';

Im just gona have to keep changing ;)

template<typename type>
int CountDidgets(type val, bool signInc = 0) {
	int d = 1; long long c;
	if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
	else for (c= -10 ; c >= val; c *= 10) d++;
	return (signInc && c < 0) ? ++d : d;
}


Now try this:

cout << CountDidgets<unsigned int>(INT_MAX) << '\n';

Should work now :icon_wink:

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

Hi,

wt is the function that can use to count number of digits in a long integer? e.g. 12345678 -> 8 11 ->2 456 -> 3 6724 -> 4


yeah...... that's my main problem here....

rjyen
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

what about in C only? the same question I would like to raise for you,

rjyen
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

what is the function that can be used to count the number of days that a thread has been dead and buried?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You