How to return output of Serial.print(floatNum,prec) in a function ?

Hi,

I am working on NEO6MV2 GPS module where I want to store coordinated in float variables. I am using TinyGPS library.

In order to do that, first I want to add extra decimal places in float value. This can be achieved by

Serial.print(12.224, 5) (Serial.print() - Arduino Reference)

However as I said I want to store this result in variable, how can I do that ?

I cannot make

return Serial.print(12.224, 5);

This is the original function:

float print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartdelay(0);
}

Instead of directly printing the data on serial monitor, how can I store its output in a variable ?

Need help,

Thanks :slight_smile:

I agree but in the library, here is the code for calling it:

float flat, flon;
gps.f_get_position(&flat, &flon, &age);
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);

When I write

Serial.print(flat)

before

print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);

it gives me float value like nn.dd. I want it as nn.dddddd

Thats my pain :frowning:

I want to send data to HTTP server using SIM900, so I am concatenating string as>

String message;
message = "lat=";
message += flat;
Serial.println(message);

which is printing lat=nn.dd and not lat=nn.dddddd which I want

Hi,

Yes, all I needed was dtostrf(), problem solved.

Thanks :slight_smile: