fcvt.c 454 B

12345678910111213141516171819202122232425
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. char *fcvt(double x, int n, int *dp, int *sign)
  6. {
  7. char tmp[1500];
  8. int i, lz;
  9. if (n > 1400U) n = 1400;
  10. sprintf(tmp, "%.*f", n, x);
  11. i = (tmp[0] == '-');
  12. if (tmp[i] == '0') lz = strspn(tmp+i+2, "0");
  13. else lz = -(int)strcspn(tmp+i, ".");
  14. if (n<=lz) {
  15. *sign = i;
  16. *dp = 1;
  17. if (n>14U) n = 14;
  18. return "000000000000000"+14-n;
  19. }
  20. return ecvt(x, n-lz, dp, sign);
  21. }