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) (https://www.arduino.cc/en/Serial/Print)

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:

You don't get to define the number of decimal places of precision for a float variable. They always have all the decimal places no matter what you do. It is only for printing them out you get to decide how many to print, but they're always there.

For GPS coordinates, fixed point math with unsigned long is between 100 and 1000 times more precise. Why throw away all that precision on a float just to get slower more bulky code?

There's a difference between an actual number which always has an infinite number of digits behind the decimal and the written representation of a number which doesn't. What is stored in the computer is the actual number, with an infinite number of digits behind the decimal, although only a few of them may actually be accurate.

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:

Serial.print(flat, 5);

Will print it to 5 places, but don't let that fool you to how many are in the number. You're just selecting how many to print.

I'd rewrite that library to use fixed point math if I were going to use GPS.

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

Oh you want it in a String. Why didn't you just say that.

I don't know how to get precision for the String class. I try to avoid that when possible because it has a nasty habit of crashing Arduino code. To get it in a c style string use dtostrf

Hi,

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

Thanks :slight_smile: