| | |
count digits
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
•
•
•
•
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
http://www.cplusplus.com/reference/c...ath/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.
or.... 
.
c Syntax (Toggle Plain Text)
int getNumDecimalDigits(int value) { char myString[16]; sprintf(myString,"%d",value); return strlen(myString); }

.
Last edited by jephthah; Jun 4th, 2008 at 2:21 am.
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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
#include<stdio.h>
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);
}
@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.
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.
Last edited by hammerhead; Jun 4th, 2008 at 4:37 am.
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
•
•
Join Date: Mar 2008
Posts: 1,491
Reputation:
Solved Threads: 123
•
•
•
•
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
CPP Syntax (Toggle Plain Text)
// 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; }
I need pageviews! most fun profile ever :)
> 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:
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:
C++ Syntax (Toggle Plain Text)
int CountDigits(int value) { int count = 0; value = std::abs(value); do { ++count; value /= 10; } while (value != 0); return count; }
Last edited by Radical Edward; Jun 4th, 2008 at 3:40 pm.
If at first you don't succeed, keep on sucking until you do succeed.
•
•
Join Date: Mar 2008
Posts: 1,491
Reputation:
Solved Threads: 123
•
•
•
•
The results are identical
I need pageviews! most fun profile ever :)
> 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:
But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:
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:
C++ Syntax (Toggle Plain Text)
int n = CountDigits(value); if (value < 0 || includeSign) ++n; ...
C++ Syntax (Toggle Plain Text)
int n = CountDigits(value); if (value < 0) --n; ...
Last edited by Radical Edward; Jun 4th, 2008 at 4:17 pm.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- how to count integer from random number (C)
- counting digits (Assembly)
- A small problem in the output (C++)
- recursive backtracking (C)
- input bianary ASCII converted into Characters (C)
- Nasm to tasm code help (Assembly)
Other Threads in the C++ Forum
- Previous Thread: mod "%" with doubles and constants
- Next Thread: struct XX has no member named YY
Views: 3854 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






