/* Convert a float `fval' to a null terminated string `result'. Only the `iprec' most significant whole digits and the `dprec' most significat fractional digits are printed. The integer part will be padded with zeros (if pad is true) or spaces (if pad is false) if it is shorter than `iprec' digits. The floating point part will always be padded with zeros. WARNING: The whole part of `fval' must be <= 100000000 (size < 9). [code copied from COW string_util.c] */ char *ftoa(float fval, char *result, int pad, int iprec, int dprec) { int i, ival; float val = fval; if ((iprec + dprec) != 0) result[iprec + dprec + 1] = '\0'; for (i = 0; i < dprec; i++) val *= 10.0; ival = val; itoapad(ival, result, pad, iprec + dprec); for (i = (iprec + dprec); i >= iprec; i--) if (result[i] == ' ') result[i + 1] = '0'; else result[i + 1] = result[i]; result[iprec] = '.'; if (fval < 1.0) result[iprec - 1] = '0'; return (result); }
main() { char s[1000]; itoa(100, s); printf("%s\n", s); itob(100, s, 16); printf("%s\n", s); itoapad(100, s, 5); printf("%s\n", s); itoa(INT_MIN, s); printf("%s\n", s); itob(INT_MIN, s, 3); printf("%s\n", s); itoapad(INT_MIN, s, 4); printf("%s\n", s); return 0; }