-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with sprintf and float values #73
Comments
Yep, sprintf (and related functions) from Espressif do not support floating-point formatting. |
Ok that's clear. So it's not a bug, it is by 'design' |
I guess this would explain the "%!f(MISSING)" output I was getting. Took me forever to find this information, hopefully leaving that string here will improve the google results... At least I know now I'm not crazy. |
best way to convert float to string but %f not worked. char *dtostrf(double val, signed char width, unsigned char prec, char *s) /* 4 minimum string length, 2 precise; converted value to в str_temp*/ |
There is a simpler solution if you don't care about exponents in the result: sprintf("%s", String(myFloat).c_str()); The default is 2 decimal places, but you can override it, e.g.: String(myFloat, 3); // 3 decimal places. For width formatting etc, use the standard printf modifiers around %s. |
For other workarounds (with possibly smaller memory footprint, because of avoiding Strings) have a look here: http://yaab-arduino.blogspot.de/2015/12/how-to-sprintf-float-with-arduino.html (jourjines solution included). |
plese help |
Using this sketch::
include <stdio.h>
void setup() {
Serial.begin(19200);
}
void loop() {
char string[40];
string[0]=0;
sprintf(string,"Characters: %c %c", 'a',65);
Serial.println(string);
string[0]=0;
sprintf(string,"Decimals: %d %ld", 1977, 650000L);
Serial.println(string);
string[0]=0;
sprintf(string,"%s", "A string");
Serial.println(string);
string[0]=0;
sprintf(string,"floats: %4.2f %+.0e %E",3.1416, 3.1416, 3.1416);
Serial.println(string);
delay(1000);
}
Results in this output;
Characters: a A
Decimals: 1977 650000
A string
floats: %.2f %.0e %E
As you can see, the sprintf function is working for %d, %s, %c but not %f
Using latest github version, downloaded today (april 16th)
The text was updated successfully, but these errors were encountered: