1151497Sru/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2004
2151497Sru   Free Software Foundation, Inc.
375584Sru     Written by James Clark (jjc@jclark.com)
475584Sru
575584SruThis file is part of groff.
675584Sru
775584Srugroff is free software; you can redistribute it and/or modify it under
875584Sruthe terms of the GNU General Public License as published by the Free
975584SruSoftware Foundation; either version 2, or (at your option) any later
1075584Sruversion.
1175584Sru
1275584Srugroff is distributed in the hope that it will be useful, but WITHOUT ANY
1375584SruWARRANTY; without even the implied warranty of MERCHANTABILITY or
1475584SruFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1575584Srufor more details.
1675584Sru
1775584SruYou should have received a copy of the GNU General Public License along
1875584Sruwith groff; see the file COPYING.  If not, write to the Free Software
19151497SruFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
2075584Sru
2175584Sru#define INT_DIGITS 19		/* enough for 64-bit integer */
2275584Sru
23151497Sru#ifdef __cplusplus
24151497Sruextern "C" {
25151497Sru#endif
26151497Sru
27151497Sruchar *if_to_a(int i, int decimal_point)
2875584Sru{
2975584Sru  /* room for a -, INT_DIGITS digits, a decimal point, and a terminating '\0' */
3075584Sru  static char buf[INT_DIGITS + 3];
3175584Sru  char *p = buf + INT_DIGITS + 2;
3275584Sru  int point = 0;
3375584Sru  buf[INT_DIGITS + 2] = '\0';
3475584Sru  /* assert(decimal_point <= INT_DIGITS); */
3575584Sru  if (i >= 0) {
3675584Sru    do {
3775584Sru      *--p = '0' + (i % 10);
3875584Sru      i /= 10;
3975584Sru      if (++point == decimal_point)
4075584Sru	*--p = '.';
4175584Sru    } while (i != 0 || point < decimal_point);
4275584Sru  }
4375584Sru  else {			/* i < 0 */
4475584Sru    do {
4575584Sru      *--p = '0' - (i % 10);
4675584Sru      i /= 10;
4775584Sru      if (++point == decimal_point)
4875584Sru	*--p = '.';
4975584Sru    } while (i != 0 || point < decimal_point);
5075584Sru    *--p = '-';
5175584Sru  }
5275584Sru  if (decimal_point > 0) {
5375584Sru    char *q;
5475584Sru    /* there must be a dot, so this will terminate */
5575584Sru    for (q = buf + INT_DIGITS + 2; q[-1] == '0'; --q)
5675584Sru      ;
5775584Sru    if (q[-1] == '.') {
5875584Sru      if (q - 1 == p) {
5975584Sru	q[-1] = '0';
6075584Sru	q[0] = '\0';
6175584Sru      }
6275584Sru      else
6375584Sru	q[-1] = '\0';
6475584Sru    }
6575584Sru    else
6675584Sru      *q = '\0';
6775584Sru  }
6875584Sru  return p;
6975584Sru}
70151497Sru
71151497Sru#ifdef __cplusplus
72151497Sru}
73151497Sru#endif
74