Good day,
I have to convert GPS fix from ddmm (46.524220) to ddmm.mmmmm (4631.453200).
I received the value 46.524220 as a char.
I have to first split 46 and 524220
I can split 46 by doing
double latitude = atof(latString);
int deg = (int) latitude;
If I print latitude, I have 46.52.
May be I am wrong with the order to process
Now I want to have 5242200, to make the calcul
5242200 = (46.524220-46)*1000000
But I do not have to lost the number after the .
I have to convert it to float. I searched for several solutions but none let me do my task.
What do you suggest me?
double latitude = atof(latString);
int deg = (int) latitude;
double fraction_of_a_degree = latitude - deg;
double minutes = fraction_of_a_degree * 60.0;
double newLatitude = deg * 100 + minutes;
Hello John,
thank for your response but it partialy work
newlatitude return me this:
4631.45
I should keep a maximum of number afetr the . (at leat 4)
I should have
4631.4532
This is not because of atof()?
In reality, it's work.
print() limit to two digit after the.
that works excepted, I can not NOW convert a double to a char!!!!!
I tried with sprintf withouth success
char * WI968C::convertLatLon(char* latString) // Convert from ddmm to ddmm.mmmmm
{
/*
Serial.println(latString);
Serial.println(strlen(latString));
Serial.println(latString[0]);
Serial.println(latString[1]);
*/
char *result;
double latitude = atof(latString);
// Serial.println(latitude,4);
int deg = (int) latitude;
double fraction_of_a_degree = latitude - deg;
// Serial.println(fraction_of_a_degree,4);
double minutes = fraction_of_a_degree * 60.0;
double newLatitude = deg * 100 + minutes;
Serial.println(newLatitude,4);
//newLatitude;
sprintf(result,"%d",newLatitude);
Serial.println(result);
return result;
}
system
October 1, 2015, 11:37pm
5
I can not NOW convert a double to a char!!!!!
And you will NEVER be able to.
You could convert a double to a char ARRAY using dtostrf().
One of these days you'll get it.
pierrot10:
newlatitude return me this:
4631.45
I should keep a maximum of number afetr the . (at leat 4)
You can print it with Serial.print(newLatitude, 4); to display 4 digits after the decimal but that doesn't get around the fact that a float/double only has 6 to 7 significant digits. You can't expect accurate results in the third or forth decimal place. If the degree value goes over 99 you can't even expect two decimal places to be accurate.
If you want high accuracy you should be using fixed point math.
Short answer: use atof().
Long answer:
pierrot10:
I have to convert GPS fix from ddmm (46.524220) to ddmm.mmmmm (4631.453200).
I received the value 46.524220 as a char.
No, you receive it as a character string . This is not the same thing as the C++ String class, btw.
Anyway.
On the reference page Arduino - Home
There is a link to the avr libc library avr-libc: Modules
You want avr-libc: <stdlib.h>: General utilities
#include <stdlib.h>
double foo() {
char *the_string = "46.524220";
double the_number = atof(the_string);
Serial.print("the string value '");
Serial.print(the_string);
Serial.print("' gets converted to number ");
Serial.print(the_number);
Serial.println();
return the_number;
}
system
October 2, 2015, 1:29pm
8
How to convert a char to a float
Short answer: use atof().
I don't think so. Not that the thread title makes any sense.