1

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
asked Jun 15, 2016 at 14:50
2
  • 1
    There is no double. Commented Jun 15, 2016 at 14:52
  • 1
    What does Serial.println(FloatEx, 5); give you? Commented Jun 15, 2016 at 14:53

1 Answer 1

2

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.

answered Jun 15, 2016 at 15:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.