atof function not returning correct value

Hi,

I'm trying to convert a string to a float value with the atof() function. The problem is that it does not return the correct value to me. Using the example below it returns "21.123455" in stead of "21.123456".

What am I doeing wrong here?

Thanks in advance.

void setup() {
  Serial.begin(115200);   // Start seriele communicatie
}

void loop() {
  String str = "21.123456"  ;
  char carray[str.length() + 1]; //determine size of the array
  str.toCharArray(carray, sizeof(carray)); //put sensorString into an array
  float test = atof(carray);  
  Serial.println(test,6); //Edit the 6 to how many digits precision you need

  delay(500);
}

The 32-bit float of the Arduino just doesn't have more precision than that.

I tried this website: IEEE-754 Floating Point Converter
21.123456 = 0x41a8fcd6 = 21.123455047607422
The website changes the start value into ...55

Maarten123:
What am I doeing wrong here?

Welcome to the wonderful world of floats.

Thanks for the info, I was really hoping I did something very wrong here. :slight_smile: