ESP8266 Developer Zone The Official ESP8266 Forum 2015-03-05T13:48:56+08:00 https://bbs.espressif.com:443/feed.php?f=7&t=246 2015-03-05T13:48:56+08:00 2015-03-05T13:48:56+08:00 https://bbs.espressif.com:443/viewtopic.php?t=246&p=924#p924 <![CDATA[Re: please support printf for float]]>
NodeMCU firmware of esp8266 have solved this problem, it have standard vsprintf and sprintf. You could use vsprintf to get your printf.

https://github.com/nodemcu/nodemcu-firm ... /c_stdio.c

Statistics: Posted by vowstar — Thu Mar 05, 2015 1:48 pm


]]>
2015-03-04T23:59:55+08:00 2015-03-04T23:59:55+08:00 https://bbs.espressif.com:443/viewtopic.php?t=246&p=915#p915 <![CDATA[Re: please support printf for float]]>
viewtopic.php?f=7&t=44

will be fine in future for support this

;)

Statistics: Posted by rudi — Wed Mar 04, 2015 11:59 pm


]]>
2015-03-04T14:37:03+08:00 2015-03-04T14:37:03+08:00 https://bbs.espressif.com:443/viewtopic.php?t=246&p=902#p902 <![CDATA[please support printf for float]]> This is much needed, as for IoT we will be dealing with lots of floats, like temp, rainfall, voltage, etc.

I had to implement quick float function, but it's probably not covering lots of edge cases...

Code:

int power(int base, int exp){
    int result = 1;
    while(exp) { result *= base; exp--; }
    return result;
}

static char* ftoa(float num, uint8_t decimals) {
  // float to string; no float support in esp8266 sdk printf
  // warning: limited to 15 chars & non-reentrant
  // e.g., dont use more than once per os_printf call
  static char* buf[16];
  int whole = num;
  int decimal = (num - whole) * power(10, decimals);
  if (decimal < 0) {
    // get rid of sign on decimal portion
    decimal -= 2 * decimal;
  }
  char* pattern[10]; // setup printf pattern for decimal portion
  os_sprintf(pattern, "%%d.%%0%dd", decimals);
  os_sprintf(buf, pattern, whole, decimal);
  return (char *)buf;
}

Statistics: Posted by zerog2k — Wed Mar 04, 2015 2:37 pm


]]>