fork download
  1. #include <stdio.h>
  2.  
  3. /*
  4. Because lcd and serial don't support printf, and its very costly, and all we need
  5. is simple formating with a certain number of digits and precision, this ftoa is enough.
  6. */
  7.  
  8. //avoid needed stdlib.h by defining abs as a macro
  9. #define absq(amt) ((amt)<0?0-(amt):(amt))
  10. //avoid needing math.h by defining powers of 10 as an array
  11. long pow10[10] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
  12.  
  13. int puts_int( //send positive integers to STDOUT via putchar(char)
  14. unsigned int num //positive integer to display
  15. ,char digits //count of digits, negative and '0' pad for trailing zeros
  16. ,char pad //character to pad with, or null
  17. ) {
  18. unsigned int i;
  19. char c;
  20. unsigned char d;
  21. d = absq(digits);
  22. while (0 < d) {
  23. i = num/pow10[--d]; //pre-increment
  24. if (d && !i) { //no digits found yet. d && for leading zero
  25. if (pad) putchar(pad); else digits--; //track digits (not) used
  26. }
  27. else { //found something
  28. c = i%10; //only want the lowest digit
  29. //Optional: Don't continue after fraction done
  30. if ('0'==pad && 0<digits && 0==c) return digits-d;
  31. putchar(c+'0'); //convert to ASCII and send
  32. }
  33. }
  34. return absq(digits)-d;
  35. }
  36.  
  37. int puts_float( //send floating point numbers to STDOUT
  38. float f //number to display
  39. , char digits //digits, negative to pad with spaces
  40. , char precision //precision, negative to keep trailing zeros
  41. ) {
  42. unsigned int a;
  43. char i=0;
  44.  
  45. if(digits>=10) {return 0;};
  46. // check for negative float
  47. if(f<0.0) { //is it negative?
  48. putchar('-');i++; //indicate
  49. f*=-1; //make it positive
  50. if (0>digits) digits++; //optional, steal digit for sign, keep length
  51. }
  52.  
  53. a=(int)f; // extract whole number
  54. i+=puts_int(a,digits,digits>0?0:' ');
  55. if (precision) {
  56. putchar('.');i++;
  57. f-=(float)a; //remove whole part
  58. f*=pow10[absq(precision)]; // promote to precision
  59. f+=0.5; // round
  60. a=(int)f; // extract whole number
  61. i+=puts_int(a,precision,'0');
  62. }
  63. return i; //count of characters
  64. }
  65.  
  66.  
  67. int main(void) {
  68. float f;
  69. //puts_int(12345,6,' ');puts("");
  70. puts_float(1.0000, -9, 4);putchar(' ');
  71. puts_float(12345678.1234, 9, 4);putchar(' '); //8 digits no precision left. 9 digits fails
  72. puts_float(100000000, 9, 4);putchar('\r'); //only 8 usable digits, but 9 digit numbers are ok.
  73. for (f=0.0001;f<0.00091;f+=0.0001) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  74. for (f=0.001;f<0.0091;f+=0.001) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  75. for (f=0.01;f<0.091;f+=0.01) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  76. for (f=0.1;f<0.91;f+=0.1) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  77. //also puts_int the return value of puts_float, which is the character used count.
  78. puts_int(puts_float(123406.789, -8, 3),4,' ');putchar(',');putchar(' ');
  79. puts_int(puts_float(123456, 8, -3),4,' ');putchar(',');putchar(' ');
  80. puts_int(puts_float(123450.789, 8, 0),4,' ');putchar('\r');
  81. puts_int(puts_float(-4.4, 8, 4),4,' ');putchar(',');putchar(' ');
  82. puts_int(puts_float(-4.4, -8, -4),4,' ');putchar('\r');
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
        1.000 12345678.000 100000000.000
0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 0.0008 0.0009 
0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 
0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 
  123406.789  12, 123456.000  10, 123450   6
-4.4   5, -      4.4000  13