-1

I have noticed that the parseFloat() function returns decimal numbers, but only up to 2 decimal places. Would it be possible for it to return the numbers with more decimal places? In case we need more precision

1
  • how did you get to that conclusion?
    – Juraj
    May 11 at 18:57

1 Answer 1

2

the parseFloat() function returns decimal numbers

No, it doesn't. It returns a binary (not decimal) floating point number.

only up to 2 decimal places

The number returned by parseFloat() has 24 significant digits. Binary digits, that is, also called “bits”. This is roughly equivalent to 7 decimal digits.

That being said, beware of Serial.print(). This function converts binary floating point numbers to decimal, and defaults to showing only two digits after the radix point. You can ask for more:

Serial.println(x, 6);  // display x with 6 digits after the radix point.

If you want to gain some basic understanding of how floating point numbers work, I recommend reading The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic.

1
  • Thanks for the explanation, very educational!
    – Libegria
    May 14 at 16:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.