I want to take the GPS coordinates like 42.1234 per 22.1234 and merge them as a string, with a separator character:

42.1234 x 22.1234

But using this code:

gps_string = (String)gps.location.lat()+' x '+(String)gps.location.lng();

I lose the last decimals and I get:

42.12 x 22.12

The only way I could see full coordinates is by specifying an extra parameter to println, like this:

Serial.println(gps.location.lat(), 6);

How can I create a string containing both two coordinates without losing decimals ? e.g 42.1234 x 22.1234

  • 1
    your question has nothing to do with GPS ... it is a question about converting a floating point number to string without losing precision
    – jsotola
    Oct 26, 2019 at 22:54
  • one thing to note, the GPS module probably outputs text, not floating point numbers
    – jsotola
    Oct 26, 2019 at 23:04
  • What GPS module have you got? What GPS library are you using?
    – Dougie
    Oct 26, 2019 at 23:26
  • @Dougie I can check when I get back on computer but like jsotola said, it is a float number conversion issue, not really speciffic to this GPS, but to Arduino.
    – adrianTNT
    Oct 26, 2019 at 23:32
  • 1
    the CStringBuilder from my StreamLib allows to build a C string (char array terminated by 0) with the same functions you use on Serial
    – Juraj
    Oct 27, 2019 at 5:40

1 Answer 1


If you want 4 decimal places, you may try something like this:

gps_string = String(gps.location.lat(), 4)
           + " x "
           + String(gps.location.lng(), 4);
  • 1
    forum.arduino.cc/index.php?topic=243660.0 suggests avoiding Strings and String() functions. Use dtostrf(val, 4, 6, buff);
    – Dougie
    Oct 26, 2019 at 23:45
  • This one works very nice (simple). If I can simplify the code a bit, I will post an answer using the dtostrf. I understand the dtostrf method is for memory/performance considerations.
    – adrianTNT
    Oct 27, 2019 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.