0

I want to get the GPS ordinates in degrees with 6 decimal accuracy. I can obtain this by creating a gps object with TinyGPS library. but In the TinyGPS example codes, its written

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

But I actually want to store this latitude degrees in a variable of type string or float. when i write this;

double Tiny_latitude = (gps.location.lat());
Serial.print("Tiny lat:");
Serial.println(Tiny_latitude);      

it returns me Tiny lat:33.55 on serial monitor. But i want 33.546600. How do I do that ?

6
  • "double lat = gps.location.lat();" but I think there is more to this question? Feb 20, 2016 at 9:46
  • double and float , both are not working. both giving me 33.55 as an answer. check my updated question ... Feb 20, 2016 at 9:49
  • double and float are the same on AVR. The answer to the number of decimals printed is here arduino.cc/en/Serial/Print. The default is two. Feb 20, 2016 at 9:57
  • Try Serial.println(Tiny_latitude, 6); But remember the max number of digits is 6-7. arduino.cc/en/Reference/Float Feb 20, 2016 at 9:58
  • O man, You're a life saver ! Thanks a bunch :) Serial.println(Tiny_latitude, 6); worked like charm. Feb 20, 2016 at 10:00

1 Answer 1

0

it returns me Tiny lat:33.55 on serial monitor. But i want 33.546600. How do I do that?

The issue is simply the number of decimal places. That can be added to the Serial.println().

double Tiny_latitude = (gps.location.lat());
Serial.print("Tiny lat:");
Serial.println(Tiny_latitude, 6);  

Now if i want to make a String "your location is \nlat:33.546600 \nlong: 75.456912" , i.e. my String variable are stored in Tiny_latitude and Tiny_longitude and want to make a string my_Location. How do i do that?

I would use sprintf() for that but as that requires some additional build parameters the easiest way is to use String. Some special care is needed to increase the number of decimal places.

void loop() 
{ 
  ...
  float latitude = 33.546600;
  float longitude = 75.456912;
  String buf;
  buf += F("your location is \nlat:");
  buf += String(latitude, 6);
  buf += F("\nlong:");
  buf += String(longitude, 6);
  Serial.println(buf);
  ...
}

Cheers!

Your Answer

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

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