floatToString.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // floatToString.h
  2. //
  3. // Tim Hirzel
  4. // tim@growdown.com
  5. // March 2008
  6. // float to string
  7. //
  8. // If you don't save this as a .h, you will want to remove the default arguments
  9. // uncomment this first line, and swap it for the next. I don't think keyword arguments compile in .pde files
  10. //char * floatToString(char * outstr, float value, int places, int minwidth=, bool rightjustify) {
  11. char * floatToString(char * outstr, float value, int places, int minwidth=0, bool rightjustify=false) {
  12. // this is used to write a float value to string, outstr. oustr is also the return value.
  13. int digit;
  14. float tens = 0.1;
  15. int tenscount = 0;
  16. int i;
  17. float tempfloat = value;
  18. int c = 0;
  19. int charcount = 1;
  20. int extra = 0;
  21. // make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import
  22. // if this rounding step isn't here, the value 54.321 prints as 54.3209
  23. // calculate rounding term d: 0.5/pow(10,places)
  24. float d = 0.5;
  25. if (value < 0)
  26. d *= -1.0;
  27. // divide by ten for each decimal place
  28. for (i = 0; i < places; i++)
  29. d /= 10.0;
  30. // this small addition, combined with truncation will round our values properly
  31. tempfloat += d;
  32. // first get value tens to be the large power of ten less than value
  33. if (value < 0)
  34. tempfloat *= -1.0;
  35. while ((tens * 10.0) <= tempfloat) {
  36. tens *= 10.0;
  37. tenscount += 1;
  38. }
  39. if (tenscount > 0)
  40. charcount += tenscount;
  41. else
  42. charcount += 1;
  43. if (value < 0)
  44. charcount += 1;
  45. charcount += 1 + places;
  46. minwidth += 1; // both count the null final character
  47. if (minwidth > charcount){
  48. extra = minwidth - charcount;
  49. charcount = minwidth;
  50. }
  51. if (extra > 0 and rightjustify) {
  52. for (int i = 0; i< extra; i++) {
  53. outstr[c++] = ' ';
  54. }
  55. }
  56. // write out the negative if needed
  57. if (value < 0)
  58. outstr[c++] = '-';
  59. if (tenscount == 0)
  60. outstr[c++] = '0';
  61. for (i=0; i< tenscount; i++) {
  62. digit = (int) (tempfloat/tens);
  63. itoa(digit, &outstr[c++], 10);
  64. tempfloat = tempfloat - ((float)digit * tens);
  65. tens /= 10.0;
  66. }
  67. // if no places after decimal, stop now and return
  68. // otherwise, write the point and continue on
  69. if (places > 0)
  70. outstr[c++] = '.';
  71. // now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
  72. for (i = 0; i < places; i++) {
  73. tempfloat *= 10.0;
  74. digit = (int) tempfloat;
  75. itoa(digit, &outstr[c++], 10);
  76. // once written, subtract off that digit
  77. tempfloat = tempfloat - (float) digit;
  78. }
  79. if (extra > 0 and not rightjustify) {
  80. for (int i = 0; i< extra; i++) {
  81. outstr[c++] = ' ';
  82. }
  83. }
  84. outstr[c++] = '\0';
  85. return outstr;
  86. }