Skip to content
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

sprintf(,"%f",); does not work, gives back %f #341

Closed
holgerlembke opened this issue May 26, 2015 · 9 comments
Closed

sprintf(,"%f",); does not work, gives back %f #341

holgerlembke opened this issue May 26, 2015 · 9 comments

Comments

@holgerlembke
Copy link
Contributor

void setup() {
Serial.begin(115200);
delay(5000);

float dog = 43.34567;

char *cat = new char[100] ;
sprintf(cat,"%f",dog);

Serial.println(cat);
}

void loop() {
delay(100);

}

Instead of a number I get "%f" back...

@chadouming
Copy link
Contributor

This is a well known issue among arduino user

On Tue, May 26, 2015, 5:22 PM holgerlembke notifications@github.com wrote:

void setup() {
Serial.begin(115200);
delay(5000);

float dog = 43.34567;

char *cat = new char[100] ;
sprintf(cat,"%f",dog);

Serial.println(cat);
}

void loop() {
delay(100);

}

Instead of a number I get "%f" back...


Reply to this email directly or view it on GitHub
#341.

@holgerlembke
Copy link
Contributor Author

And so we don't want to fix it?

Or at least document it? Perhaps create a Wiki page with "things that don't work"...

@chadouming
Copy link
Contributor

you can use this function in the meantime

http://pastebin.com/FdBFk3F4

@holgerlembke
Copy link
Contributor Author

Thanks, but coding some float to string is not the problem.

The problem is that every noob uses the printf/sprint with floats and wastes time to find the root of the problems, not really assuming that printf doesn't work with floats...

@igrr
Copy link
Member

igrr commented May 27, 2015

I think *printf style functions are not really Arduino way of doing things.
String class know how to handle conversion from floats, and this is what I
see used in most example sketches, at least those supplied with Arduino.

Having floats supported in printf would be nice for some use cases though.
If you feel like rolling such an implementation, replacing printfs we have
in ROM, please go ahead. Such contribution would be appreciated.
On May 27, 2015 12:07, "holgerlembke" notifications@github.com wrote:

Thanks, but coding some float to string is not the problem.

The problem is that every noob uses the printf/sprint with floats and
wastes time to find the root of the problems, not really assuming that
printf doesn't work with floats...


Reply to this email directly or view it on GitHub
#341 (comment).

@holgerlembke
Copy link
Contributor Author

I think you are right with the String class way to go.

My "complain" is more about the lack of documentation and finding errors again and again that are well known. To others.

@ricardoquesada
Copy link

Probably due to ignorance, but I prefer to use a big snprintf than multiple String()s since my intuition tells me that String might take more resources and might be slower (just a guess).
If I have time I'll send a patch to support %f

@ghost
Copy link

ghost commented Oct 8, 2017

printf with %f not working with 2.2.0. That is fixed in beta?

@sidewaiise
Copy link

Any progress on this issue? Was trying to use a snprintf() for debugging but %<l>.<p>f , %f and %F all seem not to work.

nailyk-fr added a commit to nailyk-fr/IOT that referenced this issue Dec 24, 2018
 * As stated here esp8266/Arduino#341
   the `%f` is not working on arduino. Lets use there provided
   workaround.

Signed-off-by: nailyk-fr <nailyk_git@nailyk.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants