Results 1 to 4 of 4

Thread: Format...iomanip?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Format...iomanip?

    I have a series of double variables, that can be anything from 0.00, to 000.00. What I want is to add leading zeros so that they all line up:

    From:
    $ 5.66
    $ 12.12
    $ 122.42

    to something like:
    $ 005.66
    $ 012.12
    $ 122.42

    or better yet, a way to do:
    Code:
    $   5.66
    $  12.12
    $ 122.42
    
    (forums messed up the spacing, sorry)
    any ideas?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    jim mcnamara
    Guest
    You want this in C?
    try:
    Code:
    void lpad(char *src, char *dest, int *length){
            char *buf;
            char *s;
            memset(dest,0x0, (size_t) length +1);
            memset(dest,0x20,(size_t) length);
            buf = src;
            buf += strlen(src);
            s = dest;
            s+= length;
            while((int) buf >= (int) src){
                         *s-- = *buf--;
            }
            return;
    }

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Not sure I follow this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    jim mcnamara
    Guest
    This is an example of how to blank-fill something.

    You convert a number to a char like this:

    Code:
    void tochar(float src, char *dest){ // convert a float to a char
       sprintf(dest,"%f",src);
    }
    This left pads a char to a given length
    Code:
    void lpad(char *src, char *dest, int *length){ // fill in leading blanks
            char *buf;  // char pointer
            char *s;
            memset(dest,0x0, (size_t) length +1);  // initalize the destination
            memset(dest,0x20,(size_t) length);
            if (strlen(src)>=length){
                  strcpy(dest,src);// the src is >= length
            }
            else{  // src neds to be padded.
             buf = src; // start at the beginning of the source string
             buf += strlen(src); // go to the end
             s = dest; // start at the beginning
             s+= length; // go to the end of the source
             while((int) buf >= (int) src){
                         *s-- = *buf--; // stuff all the 
             }                          // source chars into the dest
            }                           // working back to front
            return;
    }
    usage:
    Code:
    float f;
    char *buf;
    char result[20];
     memset(result,0x00,sizeof(result);
     f = 18.3 * 4;
     tochar(f,result);
     lpad(result,19);
     result[0]='$';
     printf("%s\n",result);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width