static char *sigfig(double val, int figs, char *buf, int len)
{
  char format[80];
  int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));

  if (digitsAfterDecimal < 1)
    snprintf(format, 80, "%%.0f");
  else
    snprintf(format, 80, "%%.%df", digitsAfterDecimal);
  snprintf(buf, len, format, val);
  return buf;
}
static char *sigfig(double val, int figs, char *buf, int len)
{
  char format[80];
  int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));

  if (digitsAfterDecimal < 1)
    snprintf(format, 80, "%%.0f");
  else
    snprintf(format, 80, "%%.%df", digitsAfterDecimal);
  snprintf(buf, len, format, val);
  return buf;
}