Thread: Evaluating differ. parts of a single #.

  1. #1
    Registered User
    Join Date
    Oct 2005

    Evaluating differ. parts of a single #.

    Suppose i have an 8 digit code. I want to assign first 'x' digits to represent the first code. The 'y' digits after x i want to represent a different code. And so on.

    So for a specified number. I want the first three digits to give value to a variable. The next three to do the same to another variable. And the second last and last to both individually do the same.

    For the first three, i would:

    divide the number by 100,000. This would yield a three digit code with 5 decimals. Then i would convert the double into integer, and end up with a three digit code (called "number1") which i evaluate via if statements.

    For the next three:

    Divide the number by 100. This would create a 6 digit number with 2 decimals. I convert into integer, creating a 6 digit integer (this is "number6". Then subtract number6 - 1000*number1. This would produce a the three digits after the first three. So in 12345678, these numbers would be 456.

    However, im not sure how to do the second last and last! I was thinking of using remainders, but im unsure of how to do that.

    Please, any help or suggestions would be strongly appreciated.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    use the same math you did before... except use number7 -= ((number1*1000)+number6)*10; think about the last one. Its easy as well.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    There are two ways to get remainders.

    r = a - ((a / b) * b)

    r = 123 - ((123 / 100) * 100 )
    r = 123 - ( 1 * 100 )
    r = 123 - 100
    r = 23

    The second is to simply use the modulus operator, which does it for you:

    r = a % b;
    r = 123 % 100;

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Single Entry Single Exit
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 11-11-2007, 01:34 PM
  2. Copy defined parts from text-file
    By tomas.ra in forum C Programming
    Replies: 5
    Last Post: 02-19-2006, 01:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. concatenating single chars to multi char arrays
    By TJJ in forum C Programming
    Replies: 7
    Last Post: 11-20-2003, 04:09 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM