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:
Download the zip below.
Backup your “WString.h” and “WString.cpp” files from the folder \Arduino\hardware\arduino\cores\arduino\
Unzip the downloaded file to your \Arduino\hardware\arduino\cores\arduino\ folder overwriting the old files
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:
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!
Posted by TimeWaster Programming , Tools Arduino , class , concatination , conversion , double , float , float to string , ptatch , string , string to float 30 Comments
TimeWaster says: 09.06.2013 (June 09) at 11:14 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. Reply
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.
mantazzz says: 09.06.2013 (June 09) at 11:27 and that is my bigest problem, I don’t know how to do that… Thanks for info 😉 Reply
and that is my bigest problem, I don’t know how to do that… Thanks for info 😉
Arduinostarter1 says: 17.07.2013 (July 17) at 15:56 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 Reply
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
TimeWaster says: 17.07.2013 (July 17) at 16:18 @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: http://arduino.cc/en/Reference/HomePage in this case you have to use “concat” from the String class: http://arduino.cc/en/Reference/StringConcat Reply
@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: http://arduino.cc/en/Reference/HomePage
in this case you have to use “concat” from the String class: http://arduino.cc/en/Reference/StringConcat
kll says: 09.09.2013 (September 09) at 04:01 looks like your very good tool now is included in 1.5.3 beta == 1.0.5 beta 3 Reply
looks like your very good tool now is included in 1.5.3 beta == 1.0.5 beta 3
mikel says: 02.03.2014 (March 02) at 03:34 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? Reply
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?
TimeWaster says: 02.03.2014 (March 02) at 13:27 here you go: http://www.timewasters-place.com/wp-content/uploads/2012/01/Float_Patch_for_Arduino_1.0.3.zip Reply
here you go: http://www.timewasters-place.com/wp-content/uploads/2012/01/Float_Patch_for_Arduino_1.0.3.zip
Ray says: 11.11.2014 (November 11) at 23:56 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 :)) Reply TimeWaster says: 15.11.2014 (November 15) at 19:58 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… Reply
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 :))
TimeWaster says: 15.11.2014 (November 15) at 19:58 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… Reply
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…
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
GitHub: Chamfers for OpenSCAD
GitHub: Snake for MicroView
GitHub: TV Screen for MicroView
Thingiverse: Chamfers for OpenSCAD
Thingiverse: Pulley for Geeetech Prusa i3 B+C Idler Belt Tensioner
Devilmastah's Blog
© 2016 TimeWaster's Place