fork(3) 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. If digits is negative, it will pad left.
  7. */
  8. #define BUF_LEN 100
  9. char buf[BUF_LEN]; //need a buffer to hold formatted strings to send to LCD
  10. #define absq(amt) ((amt)<0?0-(amt):(amt))
  11.  
  12. int ftoa( char * str, //buffer to hold result.
  13. float f, //input value
  14. char digits, //total number of digits
  15. char precision //digits right of decimal
  16. ) {
  17. long pow10[10] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
  18. char i=0,k,l=0;
  19. unsigned int a,c;
  20. unsigned char b;
  21. char decimal='.';
  22. char pad=' '; //could be a parameter to support custom padding character.
  23.  
  24. if((sizeof(pow10)/sizeof(pow10[0]))<=absq(digits)) {//larger pow10 table needed
  25. str[i++]='O';
  26. str[i]='\0';
  27. return i;
  28. };
  29. // check for negative float
  30. if(f<0.0) { //is it negative?
  31. str[i++]='-'; //indicate
  32. f*=-1; //make it positive
  33. (0<digits?digits--:digits++); //optional, steal digit for sign, keep length
  34. }
  35. a=(unsigned int)f;// extracting whole number
  36. f-=a; // extracting decimal part
  37. k = digits;
  38. // number of digits in whole number
  39. while(k>0) { //note this doesn't happen if digits was negative: Padding.
  40. c = pow10[k];
  41. c = a/c;
  42. if(c>0) { break; }
  43. k--;
  44. } // number of digits in whole number are k+1
  45. /*
  46. extracting most significant digit i.e. right most digit , and concatenating to string
  47. obtained as quotient by dividing number by 10^k where k = (number of digit -1)
  48. */
  49. for(l=absq(k);l>=0;l--){
  50. c = pow10[l]; //get the next power
  51. b = a/c; //get the next digit
  52. if (b>0) pad='0'; //stop padding after first digit.
  53. if (b>10) b='e'-'0';//overflow
  54. str[i++]=(l&&!b?pad:b+'0'); //digit or pad, l&& adds leading zero for fractions
  55. a%=c; //modulo by power
  56. }
  57. if (precision) {str[i++] = decimal;};
  58. /* extracting decimal digits till precision */
  59. if (0>precision) {k=0; precision=0-precision;}
  60. for(l=precision;0<l;l--) {
  61. f*=(float)10.0;
  62. b = (int)(f); //math floor so round half way
  63. str[i++]=b+'0'; //convert to ASCII
  64. f-=(int)f;
  65. if (k && 0.00001>f) { break; } //nothing left, save chars.
  66. //0==f won't work if there are any floating point errors.
  67. }
  68. str[i]='\0'; //null terminate the buffer
  69. return i;
  70. }
  71.  
  72.  
  73. int main(void) {
  74. int i;
  75. float f;
  76. i=0;
  77. for (f=0.0001;f<0.00091;f+=0.0001) {
  78. ftoa(buf+i, f, 5, -4);buf[i+6]=' ';i+=7;buf[i]=0;
  79. }
  80. puts(buf);
  81. i=0;
  82. for (f=0.001;f<0.0091;f+=0.001) {
  83. ftoa(buf+i, f, 5, -4);buf[i+6]=' ';i+=7;buf[i]=0;
  84. }
  85. puts(buf);
  86. i=0;
  87. for (f=0.01;f<0.091;f+=0.01) {
  88. ftoa(buf+i, f, 5, -4);buf[i+6]=' ';i+=7;buf[i]=0;
  89. }
  90. puts(buf);
  91. i=0;
  92. for (f=0.1;f<0.91;f+=0.1) {
  93. ftoa(buf+i, f, 5, -4);buf[i+6]=' ';i+=7;buf[i]=0;
  94. }
  95. puts(buf);
  96. ftoa(buf, .12345, 0, 4);buf[6]=' ';
  97. ftoa(buf+7, .12345, 0, 3);buf[12]=' ';
  98. ftoa(buf+13, .12345, 0, 2);buf[17]=' ';
  99. ftoa(buf+18, .12345, 0, 1);buf[21]=' ';
  100. ftoa(buf+22, .12345, 0, 0);puts(buf);
  101. ftoa(buf, -4404.322, -8, -4);puts(buf);
  102. ftoa(buf, 4000123.1014, -3, 3);puts(buf);
  103. ftoa(buf, 4000123.1014, 9, 3);puts(buf);
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
0.0000 0.0001 0.0002 0.0003 0.0004 0.0005 0.0007 0.0007 0.0009 
0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 
0.0099 0.0199 0.0299 0.0399 0.0500 0.0600 0.0700 0.0799 0.0900 
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 
0.1234 0.123 0.12 0.1 0
-    4404.3217
e123.0
4000123.0