float, double, atof, arduino

hello ,
i have a new problem with my project

i got two gps data and now i will calculate real location cause the recieved data works with degress and minutes ,
so i found this to calculate it:

double GPRMC2Degrees = Int(Value / 100) + (Value - (Int(Value / 100) * 100)) / 60

it works fine but the arduino i'm working with only gives me two spaces after comma but i need more .
i made two chars with the gps information in it:

char Longitude="5131.1922"
char Latitude="00729.3124"

now i use atof:

float value=atof(&Longitude);
float value1=atof(&Latitude);

after using the function GPRMC2Degrees it only gives me:

51.52 and 7.49

but when i calculate it on calculator i got this

51.519873333 and 7.48473333

this is what i need for more accuracy

could someone help me ? Is there any way to say give me 4 spaces after comma??

thx for help

after using the function GPRMC2Degrees it only gives me:

The value returned by the function is not truncated to two decimal places. You may only be printing the value to two decimal places. Impossible to say from the snippets you posted, especially when the Arduino does not have a function called Int().

this is my code :

#include <string.h>
#include <ctype.h>
 
  int ledpin = 13;     // LED test pin
  char speicher[80];
  char Breitengrad[10];
  char Laengengrad[11];
  char *ptr;
  char *ptr2;
  int i;
  
  
  void setup(){
    
    pinMode(ledpin, OUTPUT);
    Serial.begin(4800);
    Serial1.begin(4800);
    
  }
  
   void loop(){
    
    auslesen();
    double peter=atof(Breitengrad);
   
    double peter1=atof(Laengengrad);

 
    double b=(int(peter / 100) + (peter - (int(peter / 100) * 100)) / 60);
     Serial.println(b);
}





void auslesen ()
{digitalWrite(ledpin,HIGH);
    
     while (Serial1.available())
     {
      speicher[i] = Serial1.read();
     
      if (speicher[i++] == 10) // überprüfung einer neuen Zeile ( == 10 ist wie '\n')
     { 
      speicher[i-1] = '\0'; // Ende einer Zeile -> Ende des Strings
      if (speicher[0] == '

&& speicher[1] == 'G' && speicher[2] == 'P' && speicher[3] == 'R' && speicher[4] == 'M' && speicher[5] == 'C')
    {
      if(speicher[18]=='A') // ist der status ok mach weiter (an 18ter Stelle steht ein "A")
      {
      ptr=&speicher[20];
      ptr2=&speicher[32];
      strncpy(Breitengrad,ptr, 9);
      strncpy(Laengengrad,ptr2,10);
      Serial.println(Breitengrad);
      Serial.println(Laengengrad);
     
      }
      else
      {
        Serial.println("Wait for Valid signal...");
      }
    }
    i=0; // Den Positionszeiger wieder auf den Anfang des Arrays setzen
    }
  }}

     Serial.println(b);

The print() method has optional arguments. The effect depends on the type being printed. For floats, the default behavior is to print two decimal places. There is an optional argument to change that behavior, documented on the Serial.print() page. You could have looked it up.

thank you :slight_smile:

Ben1306:

//...

char Breitengrad[10];
  char Laengengrad[11];
//... 
    double peter=atof(Breitengrad);
    double peter1=atof(Laengengrad);
    double b=(int(peter / 100) + (peter - (int(peter / 100) * 100)) / 60);
}

[/quote]

  1. Arduino double is the same accuracy as float (32 bit single precision) which is quesationable for GPS coordinates, but better than your experience.
  2. Are you sure that [tt]char Longitude="5131.1922";[/tt] means 51° 31' 19.22" or does it mean  51° 31.1922'  ?
  3. I fear your integer arithmetic truncating / rounding does not work as expected.
    Did you check the result of [tt]peter - (int(peter / 100) * 100)[/tt]  ( 31.1922 )?
    Does[tt] int(peter / 100)[/tt] round or truncate ???
    Does arduino do a float division when it's about  [tt]peter / 60[/tt] ?

Better have more intermediate variables with the correct type ( end eventual debug output )