gardner:
...
I would have thought there should be an AVR LIBC ...
dtostrf() does the deed for "straight" floating point, and dtostre() (just above the dtostrf() in the link that ramo102 gave) does it for scientific notation. (That link was also given in the thread that ramo102 had previously shown us.)
// Demo of avr-libc dtostrf() and dtostre()
//
// davekw7x
void setup()
{
Serial.begin(115200);
float x = -1.23456;
byte precision = 4; // Number of digits after the decimal point
// Make room for your floating point number, however you
// plan to format it.
char floatBuffer[20];
// Make room for entire print line, however long you plan
// to make it.
char printBuffer[80];
// Width of floating point field includes places for
//
// The sign
// One digit before the decimal point
// The decimal point
// Digits after decimal point (== precision)
// (Don't forget that the buffer has to hold the
// terminating zero byte.)
// Bottom line: The size of floatBuffer must be at least
// precision+4. The "width" field in the call do
// dtostrf should be at least precision plus 3 and at most
// the size of floatBuffer minus 1
//
dtostrf(x, precision+3, precision, floatBuffer);
// You could just print the raw value. (You might
// have other Serial.print() statements to give it some
// user-friendly context.)
Serial.println(floatBuffer);
// Or you could use sprintf to create an entire line in a
// different buffer. Then you could use Serial.print
// or LiquidCrystal::print, or whatever, to emit
// the whole enchilada in one statement.
sprintf(printBuffer, "With %%.%df precision, x = %s", precision, floatBuffer);
Serial.println(printBuffer);
// Width of scientific field includes places for
//
// The sign.
// One decimal digit before the decimal point.
// The decimal point.
// Some digits ofter the decimal point (== precision).
// The 'e' character for exponent.
// The sign of the exponent.
// Two places for the exponent value.
// (Don't forget that the buffer has to hold the
// terminating zero byte.)
// Bottom line: The size of floatBuffer must be at least
// precision + 8
//
// Print out just the value
dtostre(x, floatBuffer, precision, NULL);
Serial.println(floatBuffer);
// Print out the value with some other stuff from sprintf
sprintf(printBuffer,"With %%.%de precision, x = %s", precision, floatBuffer);
Serial.println(printBuffer);
}
void loop()
{
}
Output:
-1.2346
With %.4f precision, x = -1.2346
-1.2346e+00
With %.4e precision, x = -1.2346e+00
By converting to a C-style"string" in an array rather than printing it within some function, you can send it to an LCD or over a network connection, or do whatever you usually do with "strings."
Regards,
Dave
Footnote:
These functions also handle special non-number values like "inf" that can occur with floating point overflow or things like "not a number" that can occur in certain operations...
Change the example to something like:
.
.
.
float x = -1.23456;
float y = x / 0.0;
.
.
.
dtostrf(y, precision+3, precision, floatBuffer);
.
. // Use Serial.print or sprintf, or whatever, with this value
.
dtostre(y, floatBuffer, precision, NULL);
.
. // Use Serial.print or sprintf, or whatever, with this value
.
And you can see stuff like
With %.4f precision, x = -INF