home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / libgroff / iftoa.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  2.0 KB  |  76 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  */
  7.  
  8. #ifndef lint
  9. static char sccsid[] = "@(#)iftoa.cc    6.3 (Berkeley) 5/8/91";
  10. #endif /* not lint */
  11.  
  12. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  13.      Written by James Clark (jjc@jclark.uucp)
  14.  
  15. This file is part of groff.
  16.  
  17. groff is free software; you can redistribute it and/or modify it under
  18. the terms of the GNU General Public License as published by the Free
  19. Software Foundation; either version 1, or (at your option) any later
  20. version.
  21.  
  22. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  23. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  24. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  25. for more details.
  26.  
  27. You should have received a copy of the GNU General Public License along
  28. with groff; see the file LICENSE.  If not, write to the Free Software
  29. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  30.  
  31. #include "lib.h"
  32.  
  33. const char *iftoa(int i, int decimal_point)
  34. {
  35.   /* room for a -, 10 digits, a decimal point, and a terminating '\0' */
  36.   static char buf[INT_DIGITS + 3];            
  37.   char *p = buf + INT_DIGITS + 2;
  38.   int point = 0;
  39.   buf[INT_DIGITS + 2] = '\0';
  40.   /* assert(decimal_point <= INT_DIGITS); */
  41.   if (i >= 0) {
  42.     do {
  43.       *--p = '0' + (i % 10);
  44.       i /= 10;
  45.       if (++point == decimal_point)
  46.     *--p = '.';
  47.     } while (i != 0 || point < decimal_point);
  48.   }
  49.   else {            /* i < 0 */
  50.     do {
  51.       *--p = '0' - (i % 10);
  52.       i /= 10;
  53.       if (++point == decimal_point)
  54.     *--p = '.';
  55.     } while (i != 0 || point < decimal_point);
  56.     *--p = '-';
  57.   }
  58.   if (decimal_point > 0) {
  59.     char *q;
  60.     /* there must be a dot, so this will terminate */
  61.     for (q = buf + INT_DIGITS + 2; q[-1] == '0'; --q)
  62.       ;
  63.     if (q[-1] == '.') {
  64.       if (q - 1 == p) {
  65.     q[-1] = '0';
  66.     q[0] = '\0';
  67.       }
  68.       else
  69.     q[-1] = '\0';
  70.     }
  71.     else
  72.       *q = '\0';
  73.   }
  74.   return p;
  75. }
  76.