Char [] to double or String.ToDouble()

BrianTee

Member
First time poster here - long time lurker.

I am parsing an nmea sentence and require the full double precision of the latitude and longitude. Using the Teensy 4.1 for Precision Agriculture GPS and absolutely require the precision, floats will not do. We are attempting to put basic operation of a windows based application called AgOpenGPS on the teensy to see how far it can be pushed. Its an open source application used by farmers all over the world.

The ascii data for latitude and longitude is in a char array now from the text nmea sentence all parsed and ready to convert to double.

Does anyone know of an example snippet that would convert a char array to a double? Easy to do to float with atof etc, but have come up blank for double.

char longitude [15] = "12345.1234567" to a double variable.

Or does anyone have a link to the source code for atof? Perhaps i can modify that to work with double.


Is there any plan to add toDouble() to the String functions considering the true double architecture of the 4.1? I notice this question was asked in 2019.

For example:

String nmeaLongitude = "1234.1234567";

double longitude = nmeaLongitude.toDouble();

Would really prefer not using Strings, but if it is available, would use it.

Thanks in advance.
 
strtod
https://www.cplusplus.com/reference/cstdlib/strtod/

use it like this

Code:
char longitude [15] = "12345.1234567"
char* ptr; // just a dummy value needed for strtod

double val = strtod(longitude, &ptr);

Success! First is the double, second number is the text.

5325.8518996, 5325.8518996
5325.8518299, 5325.8518299
5325.8517603, 5325.8517603
5325.8516906, 5325.8516906

1x10e3 Thank you's.

Edit, this works as well:
Code:
double val = strtod(longitude, NULL);
 
Last edited:
Back
Top