serialprint a string with a number format

Hi all,

This a noob doubt, and someone have to done this before, but right now I don't have more stamina for this particular case. :sleeping:

I need to print a string with a formatted value in it... in C++ this would be easy like:

printf( "example value is: %d ?", value );

Though in arduino I can't find a similar case.
Right now i have:

Serial.println("example value is: " + value + "?");

This does not give me a fixed significant digits. the solution I have, is:

Serial.print("example value is);
Serial.print(value,3);
Serial.println("?");

Is there a most efficient way of doing this?
Serial.print(value,3);
Serial.println("?");

Is there a most efficient way of doing this?

Your solution is fine. I feel your frustration with the formatted strings, but I've found sprintf to be buggy on Arduino and not worth the hassle.

I have in the past coded my own sprintf, but it bloats up code for the sake of convenience.

in C++ this would be easy

The Arduino environment is C++ with a few exceptions so what is wrong with using sprintf() apart from the fact that it gobbles up memory ?

I've found sprintf to be buggy on Arduino

Can you provide an example ? To save memory the Arduino implementation does not support the formatting of floats but this is a conscious decision and not a bug.

"It's not a bug, it's a feature!" :wink:

I think it was the lack of support for %f that made me part ways with it. Understandable though, as memory is a limited resource.

Would be nice to be able to throw in formatted strings to Serial.print(), but again, without a scratch buffer (memory!) how could you do it.