Thread: clearing decimal 0s in ints

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    103

    clearing decimal 0s in ints

    Hey, I just have a quick question that I can't find online.

    let's say you have

    Code:
    int a=00050;
    is there a way to delete those unimportant 0's so that you are left with 50? it keeps the important 0 marker but deletes the unimportant ones?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yep.

    Code:
    int a=50;
    all thanks to my secret decoder ring.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    hmm maybe i should be clearer

    what will happen is there will be

    a char* test that takes in a number, and then i atoi(test) it to become an int

    then i %10, /10 so i can store it in a linked list, each separate digit

    those 0s will get stored, but i dont want them to. so how do i get rid of those 0s

    btw they will be stored backwards so i cant just delete every 0 until it reads the 5

    it needs to be like, read the singles 0, then the tens 5, then somehow know to delete the rest of zeros, so im wondering if theres a way to just get rid of them

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Once you convert the number to an int, the trailing 0's make no difference.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    but it does

    i am putting in 002 and when i print my int it prints out 2 but when i print out the linked list it comes out to 0->0->2

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Post your code. I suspect you're creating your linked list according to the length of the char array instead of stopping when there are no more decimal digits to be represented in the int.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    Code:
    
    struct integer* convert_integer(char* stringInt) {
       //not really in my funct: : :       char* stringInt=deleteZeros(stringInt);
       int test=atoi(&stringInt[0]);
       struct integer* list=createNode(test%10);
       int k;
       for(k=1;k<strlen(stringInt);k++) {
          test/=10;
          list=insertBack(list,(test%10));
       }
       return list;
    
    }
    this is my function attempt but i know its wrong; btw the above function is indeed taking the number backwards; that's intended

    Code:
    char* deleteZeros(char* stringInt) {
       char* solve;
       int k=strlen(stringInt);
       while(k>0) {
          if(stringInt[k]==0) {
             stringInt[k]==NULL;                   
             k--;
          }
          else {
             return stringInt;  
          }
       }
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    for(k=1;k<strlen(stringInt);k++) {
    Yeah - that's your problem. You're working with the int based on how many characters are in the string. Perhaps a better solution would be to loop as long as test > 0.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    ahh thank you, now my code is currently

    Code:
    struct integer* convert_integer(char* stringInt) {
       int test=atoi(&stringInt[0]);
       struct integer* list=createNode(test%10);
       int k;
       //for(k=1;k<strlen(stringInt);k++) {
       while(test>0) {
          test/=10;
          list=insertBack(list,(test%10));
       }
    
    //MARKER
       struct integer* current=list;
       while(current->next!=NULL) {
          current=current->next;                  
       }
       if(current->digit==0)
          free(current);
    //END MARKER
    
       return list;
    }
    the code works mostly, thanks for the advice but when i run 000250 and then i print the linked list it prints 0250; so i tried to get rid of the last 0 using the code encased by MARKER; heheh it must be horribly written code cuz it just doesnt stop running :P infinite looop...somehow

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Try
    Code:
    while(test >= 10)
    or something like that.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    ahhh wow you are right; thanks for the helpp :P :P im trying to read through it but i dont quite get the logic behind that while parameter but i'll look over it again; thanks so much XDD now all i have left is the subtract method haha thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 08:10 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM