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. updated, although there was no apparent change to the string class (only whitespaces), you could just have overwritten it with the 1.0.2 version.

    @srah1: What you have to do is rather simple: Get “winmerge” (open source), and merge the old and new versions of the files.
    I will try to contact the Arduino team, altough they have no official spot to suggest such changes. Wish me good luck.

  2. float StrToFloat(String str){
    char carray[str.length() + 1]; //determine size of the array
    str.toCharArray(carray, sizeof(carray)); //put sensorString into an array
    return atof(carray);

  3. Hello,

    I am using your patch in a project for school and wanted to give your float and string method credit because it plays a pretty vital role in the software. How would you like me to do so?


    • i am delighted to see that my patch is put to good use!

      I thought you could maybe take a couple of pictures of the project and write a short text describing the project so i can present it on my page.

      Also if you have a website for the project we could link to each other.

  4. what arduino version is updated? I tried to change the original arduino file with Your files but toFloat() function didn’t work…

  5. the actual version is valid from 1.0.3 to 1.0.5, i just checked it.
    are you sure that you used it the correct way? (that the variable you want to convert is of type String instead of type char for instance?)

    String example = “0.123”;
    float example2 = example.toFloat();

  6. I’m working with diferent kind of string… It looks like this: “STARTASX:9.61511Y:-0.99599K:6.79433Skristi:0AUKSTIS:21E”. And if I understand right, Your patch don’t extract floats, just convert string to float

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.