static char *flt(char *str, double num, int size, int precision, char fmt, int flags) { char tmp[80]; char c, sign; int n, i; // Left align means no zero padding if (flags & LEFT) flags &= ~ZEROPAD; // Determine padding and sign char c = (flags & ZEROPAD) ? '0' : ' '; sign = 0; if (flags & SIGN) { if (num < 0.0) { sign = '-'; num = -num; size--; } else if (flags & PLUS) { sign = '+'; size--; } else if (flags & SPACE) { sign = ' '; size--; } } // Compute the precision value if (precision < 0) precision = 6; // Default precision: 6 else if (precision == 0 && fmt == 'g') precision = 1; // ANSI specified // Convert floating point number to text cfltcvt(num, tmp, fmt, precision); // '#' and precision == 0 means force a decimal point if ((flags & SPECIAL) && precision == 0) forcdecpt(tmp); // 'g' format means crop zero unless '#' given if (fmt == 'g' && !(flags & SPECIAL)) cropzeros(tmp); n = strlen(tmp); // Output number with alignment and padding size -= n; if (!(flags & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' '; if (sign) *str++ = sign; if (!(flags & LEFT)) while (size-- > 0) *str++ = c; for (i = 0; i < n; i++) *str++ = tmp[i]; while (size-- > 0) *str++ = ' '; return str; }
static char *flt(char *str, double num, int size, int precision, char fmt, int flags) { char tmp[80]; char c, sign; int n, i; // Left align means no zero padding if (flags & LEFT) flags &= ~ZEROPAD; // Determine padding and sign char c = (flags & ZEROPAD) ? '0' : ' '; sign = 0; if (flags & SIGN) { if (num < 0.0) { sign = '-'; num = -num; size--; } else if (flags & PLUS) { sign = '+'; size--; } else if (flags & SPACE) { sign = ' '; size--; } } // Compute the precision value if (precision < 0) precision = 6; // Default precision: 6 // Convert floating point number to text parse_float(num, tmp, fmt, precision); if ((flags & HEX_PREP) && precision == 0) decimal_point(tmp); if (fmt == 'g' && !(flags & HEX_PREP)) cropzeros(tmp); n = strnlen(tmp,256); // Output number with alignment and padding size -= n; if (!(flags & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' '; if (sign) *str++ = sign; if (!(flags & LEFT)) while (size-- > 0) *str++ = c; for (i = 0; i < n; i++) *str++ = tmp[i]; while (size-- > 0) *str++ = ' '; return str; }