![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
counting digits
Hey,
im writing some code for padding files. Im wondering how i can count the number of digits in an integer... i.e. int a = 232; int b = 9343; int c = 23; int d = 1; a would have 3 digits, b would have 4 digits, c would have 2 and d would have 1 digit. i've looked all over - all i can find is bitwise stuff - thats not what i want - the number of set bits is not always the same as the number of digits in the integer... hope someone can help! thanks! ![]() |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
Well you have one obvious option which would be convert to a string and count the characters (possibly accounting for leading zeros depending on your task).
Another option might be to test the int against being less than 10, 100, 1000, etc. The max size for a int will mean you won't have to perform countless comparisons. Finally you could look into log's, particularly log10 of the int.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
Hey Eoin,
Thanks for the quick reply! Can you explain the logs a bit more please? Thanks! |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
You can divide it by ten until it reaches 0. Like :
213 / 10 = 21 21 / 10 = 2 2 / 10 = 0 So you have 3 numbers.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
hey sweet!
ok so obviously using log10 - i would have to use the ceiling function? for example, say our integer is 235, log10 returns 2.37 - and there are actually 3 numbers in the integer, so use celing(2.37) to get 3... that right? |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
ok so this works:
//number of digits
inline int num_digits(int num) {
return ceil(log10(num));
}but lots of warnings! :/ thanks guys! |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
I tried in the windows calculator log555 and the result is 2.744etc. So i guess this was not what eoin ment.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
I have a problem with overkilling the simple things I guess... So I won't kill all of your fun, I'll leave the leading zero issue up to you to solve (if there is even a potential for it to be a problem)
![]() #include <iostream>
#include <sstream>
int CountDigits (int source);
std::string itos (int num);
int main (void)
{
int x = 0;
int y = 12345;
x = CountDigits(y);
std::cout << "There are " << x << " digits in " << y << std::endl;
std::cin >> y;
return 0;
}
int CountDigits (int source)
{
int count = 0;
std::string z = itos(source);
return z.length();
}
std::string itos (int num)
{
std::ostringstream myStream;
myStream << num << std::flush;
return (myStream.str());
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
Sorry if this question seems stupid but why do you have to write this :
std::cin >> y;
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
Hi pegasus001, that result is perfectly ok. log555 = 2.744 implies 10^2.744 = 555. It's just a convenient fact that after rounding up the answer gives you the number of digits.
So for example log_2(x) {here I'm saying log to the base b of x) gives you the number of binary digits which would be necessary to hold x. rvm, the warnings are because log10 take and returns a float or double. So you should specify the cast manually and ideally account for when there would be an overflow. Boost.numeric_cast can do this nicely. P.S. a solution based on pegasus' suggestion of dividing by ten would probably be the most optimal as the log functions are very expensive.
__________________
Visit my website BinaryNotions. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using maths to split an integer into it's seperate digits? | bivhitscar | C | 12 | Feb 23rd, 2006 12:31 AM |
| Counting the Days.... | Ghost | C# | 2 | Dec 9th, 2005 12:33 AM |
| Counting occurrence of numbers in C | stabule | C | 6 | Nov 15th, 2005 11:49 AM |
| Showing more digits | BebopFusion | C | 4 | Jul 15th, 2005 4:57 PM |
| Help with a counting problem. | AngelX | Visual Basic | 4 | Apr 2nd, 2005 7:04 PM |