I came to this thread hoping I would find some code to use as I need a function like Ben, but in my case to reduce a sketch size, display right justified parameters on a TFT and build strings for JSON messages. The TFT does support the print() methods but at a cost of code size.
print() also has an annoying habit of putting a - sign in front of a rounded up small negative number e.g -0.00 which looks "untidy" to me. This function does not do that deliberatlely.
This is the final solution I am using, as it was written whilst consuming some homemade mead the usual caveats apply!
/* Function to convert a float (or double) type to a string array
* The value will be printed to 2 decimal places
*/
char* dec2str(float value)
{
char str[24]; // Increase if the "units" string is lengthened
int ptr = 0; // Array member pointer
if (value < -0.005) { str[ptr] = '-'; ptr++; } // Check sign and append
// else {str[ptr]='+'; ptr++;} // Optionally add + sign at beginning
// str[ptr]='
For my needs I add the parameter "units" to the end of the string, it would also be easy to add $ or + at the start, see commented out lines.
Attached is a test sketch that checks some "corner/boundary/break it" cases of the functions and compares them to the print() output.
Also attached are a few test sketches to compare compiled code sizes of a minimalist sketch.
In summary the code sizes were:
Program size
2081 bytes: print an "int"
3646 bytes: print a "float"
3280 bytes: use dtostrf() and print a character array
1996 bytes: use dec2str() function above and print a character array
So it is seems like quite a code efficient solution
For compelteness I have added an example using the String class, ouch! Minimalist sketch size is now 4750 bytes. Of course we are including a lot of hidden functionality in the sketch but if it is not wanted then it is a waste of precious space.
See post #14 regarding a bug in this code.
print_float.ino (301 Bytes)
dtostrf.ino (344 Bytes)
String_test.ino (374 Bytes)
dec2str_test.ino (4.4 KB); ptr++; // Optionally add £ or $ etc for monetary values
value = abs(value); // Remove the sign
value += 0.005; // Add to round number up
value -= 0.0000000005; // Fudge factor, otherwise x.0049999999 is rounded up (value of 0 not affected)
ltoa(value, str + ptr, 10);
strcat(str, ".");
int dec = 100 * (value - (unsigned long)value);
if (dec < 10) strcat(str, "0");
ltoa(dec, &str[strlen(str)], 10);
// Could add units string passed as a parameter to function
// strcat(str," mV"); // Optionally add "units" to end of string
return str;
}
For my needs I add the parameter "units" to the end of the string, it would also be easy to add $ or + at the start, see commented out lines.
Attached is a test sketch that checks some "corner/boundary/break it" cases of the functions and compares them to the print() output.
Also attached are a few test sketches to compare compiled code sizes of a minimalist sketch.
In summary the code sizes were:
Program size
============
2081 bytes: print an "int"
3646 bytes: print a "float"
3280 bytes: use dtostrf() and print a character array
1996 bytes: use dec2str() function above and print a character array
So it is seems like quite a code efficient solution
For compelteness I have added an example using the String class, ouch! Minimalist sketch size is now 4750 bytes. Of course we are including a lot of hidden functionality in the sketch but if it is not wanted then it is a waste of precious space.
See post #14 regarding a bug in this code.
[print_float.ino|attachment](upload://f3LmQzQpyiXs0RO4EnvXpLVQa3x.ino) (301 Bytes)
[dtostrf.ino|attachment](upload://n96o4G54JodvDeXcsf8xc4KnpIw.ino) (344 Bytes)
[String_test.ino|attachment](upload://8YIewa0BGlusbxKesA6YxlyKghd.ino) (374 Bytes)
[dec2str_test.ino|attachment](upload://u8dLwGNuaX7AdhgaLaGiDzIXhvq.ino) (4.4 KB)