fork download
  1. #include <stdio.h>
  2.  
  3. char *get_number_formatted(double f)
  4. {
  5. static char buf[128]; // this function is not thread-safe
  6. int i, j;
  7.  
  8. i = snprintf(buf, 128, "%20.10f", f) - 2;
  9.  
  10. for (j = i - 8; i > j; --i)
  11. if (buf[i] != '0')
  12. break;
  13.  
  14. buf[i + 1] = '\0';
  15. return buf;
  16. }
  17.  
  18. int main(void)
  19. {
  20. int i;
  21. for (i = -4; i < 5; ++i)
  22. printf("%5d %s\n", i, get_number_formatted(pow(10.0, i)));
  23. return 0;
  24. }
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
   -4         0.0001
   -3         0.001
   -2         0.01
   -1         0.1
    0         1.0
    1        10.0
    2       100.0
    3      1000.0
    4     10000.0