# Float or Double to String

I have searched through this forum and other places, but cannot find the answer to what I would assume to be a fairly simple process.

I am trying to get GPS numbers into a string, but I cannot. I've used both TinyGPS and the Adafruit library and they appear to use a double and a float, respectively.

I have tried to some test code for a few different techniques of moving them into string. I'll post below:

``````void setup(){
Serial.begin(115200);

}

void loop(){

double lontemp=-92.1234;
double lattemp = 31.8765;

char latt;
char lon;

dtostrf(lontemp, 4, 4, lon); // I have tried several different set's of numbers here for width and precision
dtostrf(lattemp, 2, 6, latt);

Serial.print("Latitude is: ");
Serial.println(latt);
Serial.print("Longitude is: ");
Serial.println(lon);
}
``````

output:

Latitude is: 31.876499
Longitude is:
Latitude is: 31.876499
Longitude is:

Also I have tried various different formatting techniques in sprintf , using the "ul" and "f" formatting parameters to no avail. I have written much in C since college, so I assume I am missing something easy. Thanks for any help.

The first argument is width. That is the number of characters you want in the resulting string.

The second argument is the precision - the number of characters after the decimal place.

How can you have 6 digits after the decimal point in a field with a width of 2?

For a precision of 6, the minimum width needs to be 8. That allows for only a value from 0.0 to 9.999999. If your value is greater than 10.0, the width needs to be 9. With a terminating NULL, the array needs to hold 10 characters.

``````double lontemp=-92.1234;
double lattemp = 31.8765;

char latt;
char lon;

void setup(){
Serial.begin(115200);
dtostrf(lontemp, 8, 4, lon);
dtostrf(lattemp, 8, 4, latt);
Serial.print("Latitude is: ");
Serial.println(latt);
Serial.print("Longitude is: ");
Serial.println(lon);
}//setup()

void loop(){}//loop()
``````

Note: double and float are identical (IEEE754 32 bit) for most AVR based Arduino's.

thank you all for helping me better understand that. Works perfectly now.