figuring out size of numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-04-2005
Chaplin27
figuring out size of numbers
Hey everyone,
I was curous, without using library functions, how I could go about figuring out how many digits are in a number. I'm doing this to test some user input and print in error message if they enter any number that doesn't have 4 digits. Any suggestions? Thanks a lot!
• 08-04-2005
LuckY
There are several options; I'll tell you about three (assuming integer and not floating point numbers). One is slightly abstract, one requires conversion, and one requires math.

Using math: divide by ten until the value is zero; the number of times divided by ten is the number of digits in your value. E.g., 123 / 10 = 12 (count = 1), 12 / 10 = 1 (count = 2), 1 / 10 = 0 (count = 3), 3 digits total.

Using conversion: convert the value to a string and determine the length of the string. E.g., 123 =(convert)=> "123" =(strlen)=> 3.

The abstract (and most interesting): take the logarithm of the number, cast to int, and add 1. E.g., log(123) = 2.0899051114393979318044397532233 + 1 = 3

Btw, why can't you use library functions? Instructors instructions?
• 08-04-2005
kuphryn
Another approach is to analyze the binary of the integer if you are referring to the data representation via bitwise.

Kuphryn
• 08-04-2005
ILoveVectors
or why not

if you just need to know if its 4 digits,
it prolyl be faster, just to do

Code:

`if(number <= 9999 && number >= 1000)`

as long as number is an int, it should work just fine.
• 08-04-2005
Chaplin27
the problem with that approach vectors is if the user enters 0003, which is a legal 4 digit number but fails the test your describe...just a random observation. Thanks for the reply everyone, I feel pretty good about Lucky's answer. The only thing I wonder though is say the user enters 0003, would the divide by 10 method work?

EDIT:
And the answer is no...it will not...so my new question how could I correct this bug, once again without library functions? Oh and by the way Lucky, this is just a program I was typing up for the sake of it, I want to do this without library functions. Thanks again!
• 08-04-2005
The Brain
I think in the case of numbers with leading zeros.. the only sure-fire method described so far would be the conversion to string.
• 08-04-2005
Chaplin27
I see brain...it makes the most sense...I was just trying to solve it mathematically only, without any containers...just the numbers and myself, man vs. machine!!! Whoa...got a little carried away...ahem *clears throat*...yeah I guess a string is the best option...math has failed me again....
• 08-04-2005
ILoveVectors
ahh i didnt even think of it like that.
• 08-04-2005
Zach L.
String is really the only option. You will have to read your number in as a string (otherwise, the fact that it had leading zeros is lost), but you'll still have to ensure that it is a valid number (easy using library functions ;) -- but not that hard without), and then simply count the number of characters in the string.
• 08-04-2005
Epo
Code:

```if(sizeof(MyStringNumber) == (sizeof(char) * 4)) {}```
Out of sheer curiosity (cause I'm not too good when it comes to these kinds of things), would this work?

EDIT:
• 08-04-2005
Zach L.
No... this will not work.

If the char array was statically declared within the current scope, then your sizeof will return the size of the array (times sizeof(char)), but this is not necessarily (and probably not) the size of the string. If the array was not declared in the current scope, or if it was dynamically allocated, then all you get is the size of a pointer-to-char.

Best just to look for the '\0' character.

(Of course, assuming you used the library I/O, and didn't write your own in assembly or something of that nature ;) ).

Cheers
• 08-04-2005
Epo
That makes perfect sense, and here I thought I was clever :) Ah well, another day.

Thanks for clearing it up.
• 08-05-2005
earth_angel
I was reading this thread, and just for my info,how can I convert an int to a string.
If I have an variable int x=275, I want to turn it into CString x = "275". Or a char x[] = "275".

Thanks.
• 08-05-2005
The Brain
#include<cstdlib>
int-to-ascii conversion:

Code:

```int x1 = 275; char x2[20]; itoa(x1, x2, 10); cout << "There are " << strlen(x2) << " digits in the number " << x1;```
• 08-05-2005
Daved
Why is 0003 a four digit number? It looks like a 1 digit number to me, but if your requirements are to find out how many digits typed by the user then the string version will work.

You would have to read it in as a string in the first place because if you read it in as a number and then convert it to a string you will lose the leading zeroes.

Also, if you do want to convert a number to a string, stringstreams are a more portable and safer way to do so than itoa, which is not standard.

earth_angel, if you are using a CString (which is an MFC string and so not standard anyway), use the CString Format function (e.g. x.Format("%d", 245);)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last