Thread: trunc function

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    trunc function

    Hi,


    Code:
    char format[64] = "1.51480724077861e-03";
    char buffer[64] = "";
    int number = 0;
    
    printf("format [%s]\n", format);
             if(sscanf(format, "%s", &buffer) == 1)
             {
                number = sprintf(format, "%031.15f", atof(buffer));
             }
    printf("return from sprintf [%d]\n", number);
    printf("format [%s]\n", format);
    /* OUTPUT */
    [1.51480724077861e-03]
    [31]
    [000000000000000.001514807240779]


    If I wanted the to establish the first whole number before the decimal place, what could I use ?

    In the above example, the preferred output would be 0.001514807240779


    tia,

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >number = sprintf(format, "%031.15f", atof(buffer));
    Change it to
    Code:
    number = sprintf(format, "%.15f", atof(buffer));
    If you omit any formatting for the whole number, a single 0 is printed if there is none and the whole number is printed with no leading zeros if there is.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    excellent


    thanks twm

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    stuck on sprintf again,

    (please note, that the dots are spaces but do not appear in the post, these values are all eight characters from a fixed length file)


    I was hoping to change these values

    [7.......]
    [247371..]
    [55......]

    into these values

    [.......7]
    [..247371]
    [......55]

    using this

    sprintf(output->usernbr, "%08ld", input->usernbr);

    as they are left justified values I thought turning them to longs would pad them out, but unfortunately I am getting this

    [-2684388]
    [-2684388]
    [-2684388]


    can I use sprintf for this, or does every value have to be separetely manipulated ?



    tia,

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    You can pad it out with either blanks or zero's with sprintf, it's a width specifier in the form of a number or 0number if you want leading zero's instead of blanks.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    The format specifier for printf() and sprintf() strings are %n.ms. I can never remember in total exactly how the m and n affect the output so I wrote the following program:
    Code:
    #include <stdio.h>
    #define NEXTCOL 38
    
    char *strs      = "ABCDEF";
    char *strl      = "ABCDEFGHIJKL";
    int   sizesingle= 9;
    int   size[][2]= {   3,  3,   -3,  3,    3,  9,   -3,  9,
                         3, -3,    3, -9,   -3, -3,   -3, -9,
                         3, 15,    3,-15,   -3, 15,   -3,-15,
                         0,  0,
                         9,  3,   -9,  3,   -9,  9,   -9, -3,
                        -9, -9,   -9, 15,   -9, -15,   9,  9,
                         9, -9,    9, -3,    9,  15,   9,-15,
                         0,  0,
                        15,  3,   15,  9,   15, -3,   15, -9,
                        15, 15,   15,-15,  -15,  3,  -15,  9,
                       -15, -9,  -15, 15,  -15,-15,  -15, -3,
                        -1,  0
                      };
    char buf[20];
                    
    int main(int argc, char *argv[])
    {
        int  i = 0;
        int  j;
        
        j = printf("  Short -- %d chars <%s> ", strlen(strs), strs);
        while (j++ < NEXTCOL)  putchar(' ');
        printf("  Long -- %d chars <%s>  \n", strlen(strl), strl);
        printf("\n");
        
        j = sprintf(buf,"%*s", sizesingle, strs);
        j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
        while (j++ < NEXTCOL)  putchar(' ');
        j = sprintf(buf,"%*s", sizesingle, strl);
        j = printf("       %%%3ds (%2d)  <%s> ", sizesingle, j, buf);
        printf("\n");
        
        j = sprintf(buf,"%*s", -sizesingle, strs);
        j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
        while (j++ < NEXTCOL)  putchar(' ');
        j = sprintf(buf,"%*s", -sizesingle, strl);
        j = printf("       %%%3ds (%2d)  <%s> ", -sizesingle, j, buf);
        printf("\n");
        
        j = sprintf(buf,"%.*s", sizesingle, strs);
        j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
        while (j++ < NEXTCOL)  putchar(' ');
        j = sprintf(buf,"%.*s", sizesingle, strl);
        j = printf("       %%.%2ds (%2d)  <%s> ", sizesingle, j, buf);
        printf("\n");
        
        j = sprintf(buf,"%.*s", -sizesingle, strs);
        j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
        while (j++ < NEXTCOL)  putchar(' ');
        j = sprintf(buf,"%.*s", -sizesingle, strl);
        j = printf("       %%.%2ds (%2d)  <%s> ", -sizesingle, j, buf);
        printf("\n");
        
        printf("\n");
    
        while (size[i][0] != -1)
        {
        	if (size[i][0] != 0)
        	{ 
                j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strs);
                j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
                while (j++ < NEXTCOL)  putchar(' ');
                j = sprintf(buf,"%*.*s", size[i][0], size[i][1], strl);
                j = printf("   %%%3d.%3ds (%2d)  <%s> ", size[i][0], size[i][1], j, buf);
            }
            printf("\n");
            i++;
        }
        printf("done... \n");
        return 0;
    }
    It will display a chart of all the positive & negative combinations of m & n
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    sorry chaps to be so long replying,

    I was not able to get sprintf to do it, so I had to do this,

    Code:
       ...snip...
       memcpy(output->usernbr, spacer(right_justify(input->usernbr, USERNBR_LEN, ZERO_INT), USERNBR_LEN), USERNBR_LEN);
       ...snip ...
    
    char * right_justify(char * string, int length, int pad)
    {
    char buffer[BUFFER_SIZE + NULL_SIZE];
    int count;
    int found;
    
       memset(buffer, pad, BUFFER_SIZE);
    
       for(count = length, found = FALSE; count > 0 && !found; count--)
          if(*(string + (count - 1)) != SPACE_INT)
          {
             memcpy(buffer + (length - count), string, count);
             memcpy(string, buffer, length);
             found = TRUE;
          }
    
       if(!found)
          memset(string, pad, length);
    
       return(string);
    }
    
    char * spacer(char * string, int length)
    {
    int  count = 0;
    int  found;
    
       for(count = 0, found = FALSE; count < (length - 1) && !found; count++)
       {
          if(*(string + count) == ZERO_INT)
             *(string + count) = SPACE_INT;
          else
             found = TRUE;
       }
        return(string);
    }
    this now changes these

    [7.......]
    [247371..]
    [55......]
    [247372..]
    [247373..]
    [247374..]
    [247378..]
    [110.....]
    [122.....]
    [247373..]
    [134.....]
    into these

    [.......7]
    [..247371]
    [......55]
    [..247372]
    [..247373]
    [..247374]
    [..247378]
    [.....110]
    [.....122]
    [..247373]
    [.....134]
    again, the dots are really spaces

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I was not able to get sprintf to do it
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(char *dst, const char *src, int size)
    {
       int value = atoi(src);
       sprintf(dst, "%*d", size, value);
    }
    
    int main(void)
    {
       const char buffer[][9] =
       {
          "7       ","247371  ","55      ","247372  ","247373  ",
          "247374  ","247378  ","110     ","122     ","134     ",
       };
       size_t i;
       for(i = 0; i < sizeof buffer / sizeof *buffer; ++i)
       {
          char result[sizeof *buffer / sizeof **buffer];
          foo(result, buffer[i], sizeof result / sizeof *result - 1);
          printf("[%s] -> [%s]\n", buffer[i], result);
       }
       return 0;
    }
    
    /* my output
    [7       ] -> [       7]
    [247371  ] -> [  247371]
    [55      ] -> [      55]
    [247372  ] -> [  247372]
    [247373  ] -> [  247373]
    [247374  ] -> [  247374]
    [247378  ] -> [  247378]
    [110     ] -> [     110]
    [122     ] -> [     122]
    [134     ] -> [     134]
    */
    ???
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    yes Dave, it has worked for me too

    sprintf(output->linkspeed, "%*d", LINKSPEED_LEN, atoi(input->linkspeed));

    what I did not try before, was the atoi() function


    thanks Dave,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM