I've been looking around for a ftoa function.
The
avr-libc library used with Arduino has two such conversion functions:
dtostre() for scientific notation and
dtstrf() for fixed point.
...Below is a version...
I would like to see rounded values in the output.
So, if I had a number, say, 0.014567 and I wanted three decimal places I think it should be 0.015. With four decimal places I would expect to see 0.0146. Stuff like that.
void setup()
{
Serial.begin(9600);
float x = 0.014567;
Serial.println("x was set to 0.014567");
Serial.println("Here are results with ftoa, dtostrf and dtostre");
Serial.println();
char buffer[30]; // Could be smaller, but...
for (int i = 0; i < 7; i++) {
ftoa(buffer, x, i);
Serial.print(buffer);
Serial.print(" ");
dtostrf(x, i+4, i, buffer); // avr-libc function for floats
Serial.print(buffer);
Serial.print(" ");
dtostre(x, buffer, i, NULL); // avr-libc function for scientific notation
Serial.println(buffer);
}
}
void loop() {}
// Your ftoa goes here
Output:
x was set to 0.014567
Here are results with ftoa, dtostrf and dtostre
0 0 1e-02
0.0 0.0 1.5e-02
0.01 0.01 1.46e-02
0.014 0.015 1.457e-02
0.0145 0.0146 1.4567e-02
0.01456 0.01457 1.45670e-02
0.014566 0.014567 1.456700e-02
Bottom line: It's interesting, but, as a general purpose conversion routine it has a few problems other than my preference for rounding. What happens if you give it
x = 1234567.0? That one is pretty easy to fix, I'm thinking (don't forget to test -1234567.0 also).
Regards,
Dave