•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,049 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,522 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 680 | Replies: 14
![]() |
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation:
Rep Power: 11
Solved Threads: 189
•
•
Join Date: Jan 2008
Posts: 1,490
Reputation:
Rep Power: 6
Solved Threads: 187
•
•
•
•
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/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 1:21 am.
Why so serious?
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
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 3: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.
•
•
•
•
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:
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; }
Approximately 298.60465116279069767441860465116 days until I get my first star x]
> 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:
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 2:40 pm.
If at first you don't succeed, keep on sucking until you do succeed.
•
•
•
•
The results are identical
Approximately 298.60465116279069767441860465116 days until I get my first star x]
> 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:
int n = CountDigits(value); if (value < 0 || includeSign) ++n; ...
int n = CountDigits(value); if (value < 0) --n; ...
Last edited by Radical Edward : Jun 4th, 2008 at 3:17 pm.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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



Linear Mode