I found the .toFloat() but that's not accurate enough.
String StrEx = "57.10598";
float FloatEx = StrEx.toFloat();
Serial.println(String(FloatEx)); //outputs 57.11
Serial.println(StrEx); //outputs 57.10598
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityI found the .toFloat() but that's not accurate enough.
String StrEx = "57.10598";
float FloatEx = StrEx.toFloat();
Serial.println(String(FloatEx)); //outputs 57.11
Serial.println(StrEx); //outputs 57.10598
It is a common misconception that, because when you print a float you only get 2 decimal places, that the float only has 2 decimal places.
That is not true. A float doesn't have a number of decimal places - that is how it gets its name - floating point: the decimal point floats around as needed.
The problem is actually that, by default, all the Arduino print and string conversion functions are set to 2 decimal places. Unless you specify the number of decimal places you want to print (or convert to) you get 2 decimal places.
By specifying 5 decimal places when you print or convert to a string you get 5 decimal places. It still won't always give you exactly the same number, but that is because a 32-bit float cannot store all possible numbers, so some are only an approximation. It's still a whole lot more accurate than 57.10598 -> 57.11 though.
Serial.println(FloatEx, 5);
give you?