23 Jan

Arduino: String and Float

This patch has been added to the Arduino codebase. Please do not use this patch anymore. Instead just download “Arduino 1.0.6” or “Arduino 1.5.8” or higher, all examples below will work with these versions.

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:

  1. Download the zip below.
  2. Backup your “WString.h” and “WString.cpp” files from the folder \Arduino\hardware\arduino\cores\arduino\
  3. Unzip the downloaded file to your \Arduino\hardware\arduino\cores\arduino\ folder overwriting the old files
  4. Restart your Arduino IDE

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!
Old version:
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!

30 thoughts on “Arduino: String and Float

  1. Hello!
    Thanks for patch, but now constructions like this: String(myLong, HEX) doesn’t work.

    “call of overloaded ‘String(long unsigned int, int)’ is ambiguous”

  2. To Varun and DenJS:

    Well, it happened what i warned you about: An update in the Arduino core broke my patch, which was written for Arduino 0.8.

    Since i am in Sweden right now i will rewrite the patch for Arduino 1.01 when i am back.

  3. Tried it today on 1,0.1 works OK with float… which was what I was looking for. Did not test the rest of the functionality.
    Thanks again, You made life a lot easier for me.

  4. Hey admin, could you post some guidance about what we need to do to make this work for future versions of Arduino, e.g. 1.04, 1.05 without having to ask you to do it for us each time? Also, have you reached out to the arduino team to ask them to include your changes in the standard build?

Leave a Reply

Your email address will not be published. Required fields are marked *