Arduino: String and Float   January 23rd, 2012

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!

Tags: , , , , , , , , ,
This entry was posted on Monday, January 23rd, 2012 at 19:54 and is filed under Programming, Tools. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.

30 Responses

May 10th, 2012 at 12:26
Qassim Says:

OMG…this was very helpful. Thank you very much.

July 20th, 2012 at 09:05
DenJS Says:

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

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

July 27th, 2012 at 08:43
Varun Says:

Hey mate, it did not work error was
Program1:63: error: ‘class String’ has no member named ‘toFloat’

I overwrote you files in my specified folder.

July 27th, 2012 at 13:10
TimeWaster Says:

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.

August 10th, 2012 at 19:19
TimeWaster Says:

Well, i updated the patch for 1.0.1. Have fun!

August 23rd, 2012 at 01:37
Bob Coyle Says:

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.

November 27th, 2012 at 11:08
Daniel Says:

Can you please update the patch for 1.0.2. Thanks.

November 28th, 2012 at 20:53
TimeWaster Says:

@Daniel: Done, have fun.

January 1st, 2013 at 23:11
hello Says:

Hello admin, can you update for 1.03 need this badly

January 2nd, 2013 at 00:30
srah1 Says:

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?

January 2nd, 2013 at 00:30
srah1 Says:

P.S – this is an awesome project and a great share, thanks

January 2nd, 2013 at 18:15
TimeWaster Says:

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.

January 28th, 2013 at 17:07
revi Says:

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);

February 14th, 2013 at 16:36
Mitch Says:


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?


February 17th, 2013 at 11:01
jeanmarc Says:

Thanks ! =)

February 21st, 2013 at 22:20
TimeWaster Says:

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.

February 28th, 2013 at 11:35
TimeWaster Says:

i just opened an issue on GitHub to include my patch into the next release of Arduino:

Now we can only wait.

June 7th, 2013 at 09:49
TimeWaster Says:

Good news, everyone!

my patch has finally been merged into the arduino library:

so it will most likely be added in the next version of arduino.

June 9th, 2013 at 10:44
mantazzz Says:

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

June 9th, 2013 at 10:56
TimeWaster Says:

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();

June 9th, 2013 at 11:02
mantazzz Says:

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

June 9th, 2013 at 11:14
TimeWaster Says:

you can’t put anything into the conversion, it has to be a string representation of a float, e.g.:

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

June 9th, 2013 at 11:27
mantazzz Says:

and that is my bigest problem, I don’t know how to do that… Thanks for info 😉

July 17th, 2013 at 15:56
Arduinostarter1 Says:

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

July 17th, 2013 at 16:18
TimeWaster Says:

@Arduinostarter1: the error here is that you use an AVR command (strcat) which works with chars, but the arduino implementation of String is an object, not char.

please use only commands from the arduino reference when working with String:

in this case you have to use “concat” from the String class:

September 9th, 2013 at 04:01
kll Says:

looks like your very good tool now is included in
1.5.3 beta == 1.0.5 beta 3

March 2nd, 2014 at 03:34
mikel Says:

I’m using an arduino which doesn’t yet work with 1.5 (a Teensy 3.1) any chance I can still get a link to the zip for 1.05?

November 11th, 2014 at 23:56
Ray Says:

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 , 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 :))

November 15th, 2014 at 19:58
TimeWaster Says:

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:

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