Arduino has a nice convenience Class: String.
Although it wraps nicely Chars in Arduino, it misses one really important aspect, it can’t handle Float. I really don’t get it why they didn’t add this, it is so essential in handling strings and it is really a pain in the ass to cover it for yourself since the AVR methods for converting Floats need buffer variables.
So i patched the class for myself, which was much more easy than I thought. Without any deep knowledge in C or C++ I added float and double functionality to the class and I do not understand why it wasn’t done until now. I can now do String to Float and Float to String conversions without any hassle.
If you want to use it, follow these steps:
Please note: Changing a core class is a BAD idea. Why? Because every time you update the IDE you WILL loose the patch you added. Also future changes in the Class could break the compatibility to your patch. So please consider not using my patch. I know it sounds silly. ;o)
Here are some examples for the new abilities of the String Class:
String example1 = String(1.852); // Equals: "1.852000" example1 = String(1.852, 2); // Equals: "1.85" String example2 = String(example1.toFloat() * 1.784561, 4); // Equals: "3.3014" example1 = example2 + 1.58356; // Equals: "3.30141.583560", since the example2 variable is of type String the number gets converted into String and concatinated.
The default precision i chose is 6 decimal places, but you can specify any number of decimal places as a second parameter in String(num, decimalPlaces).
I hope the guys behind Arduino fix this soon.
Float Patch for Arduino 1.0.3 to 1.0.5 See note above!
Float Patch for Arduino 1.0.2 See note above!
Float Patch for Arduino 1.0.1 See note above!
Float Patch for Arduino 0023 See note above!