How to concatenate two floats?

I'm collecting geographical data for a couple of sensors I have. They collect the latitude + longitude and store them as float, e.g.:

float lat, long;

void loop() {
....
lat = 47.45469393
lon = -118.68409211
....
}

Now I want to have a new variable, that concatenates them (only the first 4 decimals), such as:

gps = 47.4546, -118.68

automatically displayed on a map. What's the best way of doing this?
Last edited on
concatenates them as a formatted string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>
#include <iomanip> // setprecision
#include <sstream> // stringstream

int main()
{
    float lat, lon;

    lat = 47.45469393;
    lon = -118.68409211;

    std::stringstream stream;
    stream << std::fixed << std::setprecision(4) << lat << ", " << lon;
    std::string gps = stream.str();
    
    std::cout << gps << std::endl; // "47.4547, -118.6841"

}
if you need it to look exactly like what you wrote and you actually need to store the values (as opposed to just printing them), a tiny class or struct could wrap a string with a couple of methods that allowed syntax like

gps.assign(47.123,100.567);
or
gps = othergps;
or
gpstype gps(47.123,100.567);
or various other constructs.

and a vector of those could hold a bunch until you were ready to print them, etc?

if you wanted to get really fancy a small object like

struct gpstype
{
float lat;
float lon;
string latlon_txt;
//methods to provide what you need, probably 2-3 simple methods will do here?
};

Last edited on
Topic archived. No new replies allowed.