23 Jan

Arduino: String and Float

PLEASE NOTE:
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.

Download:
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. you can’t put anything into the conversion, it has to be a string representation of a float, e.g.:
    “1.234”
    “1.234E-5”
    “-1.2”
    “1”

    if you have concatenated mixed data in a string you have to separate it yourself and only convert the float parts into float.

  2. Concat strings doesn’t work with Strin(a,b):

    float sngTest= .97531;
    strcpy(strLine1, ” Pac = “);
    part2= String(sngtest,4);
    strcat(strLine1, part2 );

    Error: cannot convert ‘String’ to ‘const char*’ for argument ‘2’ to ‘char* strcat(char*, const char*)’

    Expected: Pac = 0,9753

  3. Hi TimeWaster,
    Thanks for doing this in the first place and persisting with it to the extent it is now in the main IDE – I only found it today, I’m using The mac version 1.5.5. I came here looking for documentation – although it’s listed as a method in http://arduino.cc/en/Reference/StringObject , the link goes to String.toInt ! Also in my ide at least, the .toFloat doesn’t get auto-coloured like other methods, so I thought it wasn’t going to work at all 🙂

    The documentation I was looking for was what happens if a non-numeric string is entered – is there a non-numeric result (eg NaN), but it look like zero is returned, so if I get a zero I’ll check if the first character in the string is “0”, post an error if it isn’t and accept zero as an input if it is.

    Thanks very much again, a very useful addition (which avoided me having to waste too much time :))

    • hi Ray, i’m glad it is useful for you!

      What Arduino and therefore my changes in the String class does is to abstract the underlying AVR commands so the user doesn’t has to deal with buffers, pointers and cryptic commands.
      String.toFloat is an abstraction of “strtod(const char * nptr, char ** endptr)” which documentation can be found here: http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga5ee4d110a3bb55d2eadda05e3ebedf8a

      It states: “If no conversion is performed, zero is returned” which basically means when the command gets a string that is not definitely a number it returns zero (0.0 since it is float). This means in return you cannot know if the return is an error or if the string really contained “0.0”.

      As with many AVR commands you have to be absolutely sure that the data you use in your program is what you think it is.
      Microcontrollers do not have runtime errors or debugging capability, so using uncertain data most likely results in a crashed microcontroller (undefined state).

      I hope i could help…

Leave a Reply

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