#include <ncbi.h>
#include <gish.h>
#include <gishlib.h>

static char	sbuf[256];
static CharPtr	sp0 = sbuf;

CharPtr LIBCALL
d2s(x)
	double	x;
{
	char	sb[66];
	CharPtr	sp, dp;
	int		len, i, j, d;

	if (x >= 1e64)
		return "<TOO BIG>";
	sprintf(sb, "%.0f", x);
	len = i = strlen(sb);
	d = i + i/3;
	if (d + sp0 >= sbuf + DIM(sbuf))
		sp0 = sbuf; /* wrap around */

	sp = sb + i;
	dp = sp0 + d;
	*dp = NULLB;
	sp0 += d;

	j = 0;
	while (i-- > 0) {
		if (j++ == 3 && len > 4) {
			*--dp = ',';
			j = 1;
		}
		*--dp = *--sp;
	}
	return dp;
}
