home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / BENCH / FTOA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  2.3 KB  |  98 lines

  1. /* ==( bench/ftoa.c )== */
  2.  
  3. /* ----------------------------------------------- */
  4. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  5. /* Modification to this source is not supported    */
  6. /* by Vestronix Inc.                               */
  7. /*            All Rights Reserved                  */
  8. /* ----------------------------------------------- */
  9. /* Written   Nig   1-Jan-87                        */
  10. /* Modified  Nig  31-Jan-90  See comments below    */
  11. /* ----------------------------------------------- */
  12. /* %W%  (%H% %T%) */
  13.  
  14. /*
  15.  *  Modifications
  16.  *
  17.  *  31-Jan-90  Nig - Check for Zero Length
  18.  *  11-Dec-89  Geo - V2 version
  19.  *  25-Oct-89  Geo - 1.32 Merge
  20. */
  21.  
  22. /*
  23.  * Function to Strip Off the Trailing Zeros from a string, created using a
  24.  * sprintf(str, "%lf", num). Result is returned in STR
  25.  *
  26.  * Arguments :   STR    -    String Containing Result
  27.  *               NUM    -    Number to be Converted
  28.  *               LEN    -    Max Length of String to be Returned
  29. */
  30.  
  31. # include <stdio.h>
  32. # include <bench.h>
  33.  
  34. # define    FLTSIZE    81
  35.  
  36. void f_to_a(str, num, len)
  37. char *str;
  38. double num;
  39. int len;
  40. {
  41.    char   *ptr;
  42.    char   lstr[FLTSIZE];   /* local string - sprintf needs a largish one */
  43.  
  44.     if (len == 0)
  45.         *lstr = '\0';
  46.    else
  47.         sprintf(lstr, "%lf", num);
  48.  
  49.     if (strlen(lstr))
  50.    {
  51.       ptr = strchr(lstr, '\0');
  52.       /* Strip Off the Trailing Zeros */
  53.       do
  54.       {
  55.          ptr--;
  56.       } while ((ptr > lstr) && (*ptr == '0'));
  57.       *++ptr = '\0';
  58.       ptr--;
  59.  
  60.       /* Make Sure New String is Not Too Long for its Destination Buffer */
  61.       if (strlen(lstr) > len)
  62.       {
  63.          lstr[len] = '\0'; /* truncate */
  64.          ptr = &lstr[len-1];  /* or lstr + len - 1 */
  65.       }
  66.  
  67.       /*
  68.        * Having Stripped Off Zeros & Extra Characters, if the Last
  69.        * Character is a FULLSTOP, then Blat it.
  70.       */
  71.       if (*ptr == '.')
  72.          *ptr = '\0';
  73.    }
  74.    strcpy(str, lstr);
  75. }
  76.  
  77. void l_to_a(str, num, len)
  78. char *str;
  79. long num;
  80. int len;
  81. {
  82.    char   lstr[FLTSIZE];   /* local string - sprintf needs a largish one */
  83.  
  84.    sprintf(lstr, "%ld", num); 
  85.     lstr[len] = '\0'; /* truncate to max len */
  86.    strcpy(str, lstr);
  87. }
  88.  
  89.  
  90. void i_to_a(str, num, len)
  91. char *str;
  92. int num;
  93. int len;
  94. {
  95.     l_to_a(str, (long)num, len);
  96. }
  97.  
  98.