Problem in converting to double

Hi everyone Im receiving a data by gps
for example
char lat[]="30.246687";
i need to change it to a double for calculations...

but when Im using

strtod()

I get 30.25

I need the whole number... what should i do?

If you are thinking of GPS NMEA data,

Is not a normal floating point number. The format is DDMM.MMMMM, degrees and minutes. Use TinyGPS++ to parse the data.

See What Exactly Is GPS NMEA Data? - GPS World

If that is not GPS NMEA data, use the function atof() or atod() to convert the character string to a float or double.

you probably don't. that's what you see if you print it without asking for 6 digits after the decimal point

see what you get from

char lat[] = "30.246687";

void setup() {
  Serial.begin(115200);
  double dLat = strtod(lat, nullptr);
  Serial.println(dLat, 2);
  Serial.println(dLat, 3);
  Serial.println(dLat, 4);
  Serial.println(dLat, 5);
  Serial.println(dLat, 6);
}

void loop() {}
you should see this
30.25
30.247
30.2467
30.24669
30.246686
30.2466869

note that double are really just float (IEEE 4 bytes representations) on 8 bit microcontrollers and so you won't have the exact digit at the 6th digit after the decimal point

2 Likes

Be aware that the 6th digit byond the decimal point is units of approx 10cm.

So the GPS would need to be a real good one to want to do calculations beyond that resolution.

Correction:
...note that double are really just float (IEEE 4 bytes representations) on 8 bit microcontrollers and so you won't have the exact digit at the 6th digit...

The location of the decimal point has nothing to do with the number of significant digits...

1 Like

Indeed wrong wording

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.