Look at this snippet of code (from TinyGPS++ library):

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

It prints the latitude location with 6 decimal points. I want, instead of printing that value, to have it assigned to a variable.

But i want it assigned just like above, with six decimal points. Is there a way to do this?

  • 3
    Sounds like you want dtostrf() - nongnu.org/avr-libc/user-manual/… It should do what you want (you have to preallocate the string, though). Jun 10 '14 at 1:49
  • 1
    To be clear, do you want to store the string with 6 decimal places, or a floating point value rounded to 6 decimal places?
    – BrettAM
    Jun 10 '14 at 19:09
  • Whatever will do, but i guess a string suits me better Jun 10 '14 at 19:42
  • What research have you done on this? @ConnorWolf gave you comment with a valid suggestion, have you looked into it?
    – Madivad
    Jun 11 '14 at 16:57

Merely expanding on what ConnorWolf has said (which I saw yesterday), look at the link he gave you, it gives you a run down on the command you're after.

The dtostrf() function returns the pointer to the converted string s.

char* dtostrf ( double __val, signed char __width, unsigned char __prec, char * __s )
The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.

Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.

From that, I wrote a short sketch that divides millis() by 1000 and saves this in a float, we then use this float as a source for dtostrf() to do it's work and store the result in a char array.

void setup(){

void loop(){
  char c[12];
  float f = millis()/1000.0;

I have picked arbitrary values, you're going to have to do the calculations on the size of the array to create (don't forget the leading negative sign, the digits before the decimal, the decimal and your precision digits as well as the null terminator!


I had the same problem and I found answer

char latitude[20];
char longitude[20];


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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