p kj
/
LPC824-BlynkWeatherstation
Microduino
Fork of Io_moon by
Diff: Blynk_v0_3_7/Blynk/utility/utility.cpp
- Revision:
- 0:740c1eb2df13
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Blynk_v0_3_7/Blynk/utility/utility.cpp Thu Jun 23 11:16:14 2016 +0000 @@ -0,0 +1,61 @@ +#include <Blynk/BlynkDebug.h> + +#if defined(ESP8266) && !defined(BLYNK_NO_FLOAT) +#include <string.h> +#include <math.h> + +char* dtostrf_internal(double number, signed char width, unsigned char prec, char *s) { + if(isnan(number)) { + strcpy(s, "nan"); + return s; + } + if(isinf(number)) { + strcpy(s, "inf"); + return s; + } + + if(number > 4294967040.0 || number < -4294967040.0) { + strcpy(s, "ovf"); + return s; + } + char* out = s; + // Handle negative numbers + if(number < 0.0) { + *out = '-'; + ++out; + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for(uint8_t i = 0; i < prec; ++i) + rounding /= 10.0; + + number += rounding; + + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long) number; + double remainder = number - (double) int_part; + out += sprintf(out, "%d", int_part); + + // Print the decimal point, but only if there are digits beyond + if(prec > 0) { + *out = '.'; + ++out; + } + + while(prec-- > 0) { + remainder *= 10.0; + if((int)remainder == 0){ + *out = '0'; + ++out; + } + } + sprintf(out, "%d", (int) remainder); + + return s; +} + +#endif + +