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.
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
Old version:
Float Patch for Arduino 1.0.2
Float Patch for Arduino 1.0.1
Float Patch for Arduino 0023
OMG…this was very helpful. Thank you very much.
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”
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.
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.
Well, i updated the patch for 1.0.1. Have fun!
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.
Can you please update the patch for 1.0.2. Thanks.
@Daniel: Done, have fun.
Hello admin, can you update for 1.03 need this badly
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?
P.S – this is an awesome project and a great share, thanks
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.
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);
}
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?
Thanks!
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.
i just opened an issue on GitHub to include my patch into the next release of Arduino: https://github.com/arduino/Arduino/issues/1297
Now we can only wait.