• 【自动驾驶解决方案】C++取整与保留小数位


    一、C++基础

    1.1double型保留小数为,并以字符输出

    #include 
    #include 
    #include  // 包含std::fixed
    
    int main() {
    	//浮点数
        double number = 3.1415926;
    	//转换工具类stream
        std::stringstream stream;
        stream << std::fixed << std::setprecision(2) << number;
        //c++11内置函数str()
        std::string result = stream.str();
        //输出
        std::cout << result << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    1.2 四舍五入

    常用的方法是使用std::ostringstream和std::fixed结合使用std::setprecision和std::round来实现

    #include 
    #include 
    #include  // 包含std::fixed
    #include  // 包含std::round
    
    int main() {
        double number = 3.1415926;
        
        std::ostringstream stream;
        stream << std::fixed << std::setprecision(2) << std::round(number * 100) / 100;
        
        std::string result = stream.str();
        
        std::cout << result << std::endl;
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二 自动驾驶方案

    1.1 目标跟踪部分代码

    	.
    	.
    	.
    	.
    	//获取目标距离
        double distance = cvt_point(cv::Point(center_x, center_y));
        std::stringstream stream;
        stream << std::fixed << std::setprecision(2) << distance;
        //转为有2位小数的字符
        std::string disttance_str = stream.str();
    
        // 通过opencv可视化
        cv::putText(
          image, 
          //cv::format("ID: %s", uuid_str.c_str()),
          cv::format("Dis: %s m", disttance_str.c_str()),
          cv::Point(left, top - 5),
          cv::FONT_HERSHEY_SIMPLEX,
          3,  // font scale
          color,
          10,  // thickness /home/nvidia/yolo_test/src/track
          cv::LINE_AA);
      }
      .
      .
      .
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    代码效果图,小数点只保留两位
    在这里插入图片描述

  • 相关阅读:
    SAP委外物料的BOM维护
    金仓数据库KingbaseES数据库管理员指南--17数据库调度概念
    (七)什么是Vite——vite优劣势、命令
    为什么基础架构即代码对您的业务很重要
    【EC200U】GPS定位
    【Linux】文件系统
    JavaScript 生成 16: 9 宽高比
    Coke(六):有趣的定时器任务
    android 动画中插值器Interpolator详解
    不一样的网络协议-------KCP协议
  • 原文地址:https://blog.csdn.net/puiopp63/article/details/134510631