float to string precision

I'm working with a GPS locator, and I'm trying to get the GPS location data to look right. I can't seem to get the float values to store as a string with enough precision.I have the following code which is based on the "GPS" demo sketch from ADAFruit:

if (gps_success) {

    Serial.print("GPS lat:");
    Serial.println(latitude, 7);
    Serial.print("GPS long:");
    Serial.println(longitude, 7);
    Serial.print("GPS altitude:");
    Serial.println(altitude);
    Serial.print("GPS speed KPH:");
    Serial.println(speed_kph);
    Serial.print("GPS speed MPH:");
    speed_mph = speed_kph * 0.621371192;
    Serial.println(speed_mph);
    Serial.print("GPS heading:");
    Serial.println(heading);
    
    //join string
    locationS = String(latitude,7)+ "," + String(longitude,7);
    Serial.print("String output=");
    Serial.println(locationS);

The problem is that I'm getting two different answers for "latitude" and "longitude." The output is as follows:

GPS lat:39.4110717
GPS long:-77.4046249
GPS altitude:103.80
GPS speed KPH:3.19
GPS speed MPH:1.98
GPS heading:259.40
String output=39.4110720,-77.4046250

If you notice, the "String output" line is rounded. For some reason, I can't get the number to go lower than 6 decimal places here, but I can get it to go down to 7 when I pipe the output to serial at the start of the submitted code.

I have tried using 'dtostrf,' but I get the same results. String(foo) is just shorter to write with the same output.

Why does Serial.println(foo,7) give me a different number than string(foo,7) and dtostrf(foo,13,7,*bar)?

A float doesn't support GPS precision.

A float really only has about 6 digits of precision anyway. So anything after about the fourth decimal place is not something you can count on for accuracy anyway. Those different methods are getting different answers because you're outside of the precision a float can provide. If you want high precision in GPS data you should use fixed point math with something like unsigned long.

If you want high precision in GPS data you should use fixed point math with something like unsigned long.

If you want to output the string that the GPS sent, do NOT convert the string to a float and then convert the float back to a string. Precision loss is guaranteed that way.

NeoGPS preserves the full precision of your GPS device. See the example NMEAloc.ino for how to display all significant digits without losing precision to the float conversion. NeoGPS is available from the Ardino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.