Thread: determining # of digits

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    68

    determining # of digits

    i need to fnid out how many digits make up a number.
    i thought about using modulus first but figured that wouldnt work.
    have i overlooked a built-in function ??
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: determining # of digits

    the fastest brute force way to do this is actually a long string of conditionals, but unless there's some urgent need for speed, use a loop and divide by 10, something like

    while (number > 0)
    {
    digits++;
    number = number / 10;
    }
    hello, internet!

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    what if the number is 00000. there are 5 digits.
    00000 = 0 and wont be divided by 10.
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by glowstick
    what if the number is 00000. there are 5 digits.
    no it doesnt, 00000 has 0 digits just like 0.
    hello, internet!

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    00000 has 5 digits.
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Is it user input or data within the program?
    If it's user input, capture it as a string, validate that digits have been entered, then use something like strlen to get the size of the string (or count array elements, whatever). Then use atoi or similar to turn it into a number if that's what you need.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int numlen( int num )
    {
        char buf[BUFSIZ]={0};
    
        sprintf(buf, "%d", num );
        return strlen( buf );
    }
    I'm lazy.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    Originally posted by Azuth
    Is it user input or data within the program?
    If it's user input, capture it as a string, validate that digits have been entered, then use something like strlen to get the size of the string (or count array elements, whatever). Then use atoi or similar to turn it into a number if that's what you need.
    that'll work. thanks.
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or:
    #include <math.h>
    .
    .
    int num_of_digits;
    num_of_digits = (int) log10((double) num) + 1;

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you're wanting to count the leading zero's, I can't see any amount of maths giving you the answer.... the only way, as already posted, is using a string (char array).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    Originally posted by Hammer
    If you're wanting to count the leading zero's, I can't see any amount of maths giving you the answer.... the only way, as already posted, is using a string (char array).
    thats what i have decided to do and it works lie a charm. in fact i just left it as a char array and printed as a string.
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  3. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  4. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM
  5. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:28 AM