c++ - C++ 结构中的第一个 double 自动缩短为两位小数

标签 c++ struct arduino formatting

<分区>

更新:

正如 Pete Kirkham 正确指出的那样,println 将数字缩短为两个位置。我用普通的旧打印更新了我的代码,它仍然只切断了这两个。如果您希望它有 7 个小数位,则必须放置 Serial.print(some_number, 7) 。

谢谢皮特

原帖

我是 C++ 的新手,正在编写一个 arduino GPS 应用程序,并注意到一个值不断缩短到小数点后两位,而结构中的另一个值却没有。

这是结构声明

    struct Poi {
      char name[30];
      double lat;
      double lng;
    };

这是实际的结构实例:

    Poi poi_list[3] = {
      {"AK Plaza W",36.9905263,127.0847449}
      ,
      {"AK Plaza East",36.9905263,127.0861048}
      ,
      {"Ramen Place",36.9905263,127.0895004}
    }

现在,当我尝试查看 poi_list[0].lat 或 [1].lat 或 [2].lat 时,所有值都是 36.99 并且它会切断其余部分,其中 .lng 值始终是整个数字。

整个文件在github上here

#include "TinyGPS++.h"
#include "SoftwareSerial.h"

SoftwareSerial serial_connection(10, 11); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
TinyGPSLocation loc;
const double EIFFEL_TOWER_LAT = 36.96070;
const double EIFFEL_TOWER_LNG = 127.05692;
struct Poi {
  char name[30];
  double lat;
  double lng;
};

Poi poi_list[13] = {
{"AK Plaza W",36.9905263,127.0847449}
,
{"AK Plaza East",36.9905263,127.0861048}
,
{"Ramen Place",36.9905263,127.0895004}
,
{"BX Center",36.9905263,127.0339894}
,
{"BX West",36.9905263,127.0350462}
,
{"Osan Chilis",36.9905263,127.0365375}
,
{"Work Main Gate",36.9905263,127.0219141}
,
{"Commissary",36.9905263,127.0024359}
,
{"PX Food Court",36.9905263,127.0002204}
,
{"PX Main entrance",36.9905263,126.9993058}
,
{"One Stop",36.9905263,127.0221126}
,
{"AMC",36.9905263,127.0425832}
,
{"Braii Republic",36.9905263,127.0446378}
};
int number_of_points = 13;


void setup()
{
  Serial.begin(9600);//This opens up communications to the Serial monitor in the Arduino IDE
  serial_connection.begin(9600);//This opens up communications to the GPS
  Serial.println("GPS Start");//Just show to the monitor that the sketch has started 

}

void loop()
{
  while(serial_connection.available())//While there are characters to come from the GPS
  {
    gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
  }
  if(gps.location.isUpdated())
  {
    for(int i=0;i<number_of_points;i++){
        Serial.print("Name: ");
        Serial.println(poi_list[i].name);
        Serial.print("Distance: ");
        Serial.print(calculate_distance(poi_list[i]));
    }
  }
}

float calculate_distance(Poi point){
  float distance = gps.distanceBetween(
    gps.location.lat(),
    gps.location.lng(),
    point.lat,
    point.lng);
    Serial.print("lat is ");
    Serial.print(point.lat);
    Serial.println("");
    Serial.print("longitutde is ");
    Serial.print(point.lng);
    return distance;
}

最佳答案

根据 Arduino documentation ,输出函数 Serial.println() 会将逗号后的 float 舍入为两位数:

Serial.print(1.23456) gives "1.23"

如果你想要更详细的介绍,你必须指定你想要的位数作为第二个参数:

Serial.println(1.23456, 4) gives "1.2346"

由于 Arduino 的 float 的精度只有 6-7 位,您可以选择

Serial.println(point.lat, 5);

关于c++ - C++ 结构中的第一个 double 自动缩短为两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221605/

相关文章:

c - "Expected declaration before ' struct ' "声明函数时?

c++ - 使用 Serial.Read 输入在 Arduino 上设置液晶光标

c++ - vector 、结构和 std::find

c++ - cin的地址和返回值

c++ - 无法将 ifstream 对象声明为我类(class)的成员

swift - 将数据从 Arduino 发送到应用程序

java - RXTX getPortIdentifiers() 返回空

c++ - 重载 [][] 运算符 c++

c - "structure with flexible array member shall not be a member of a structure"的基本原理是什么?

class - D 中的自动字段顺序重排