当前位置: 首页 > article >正文

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

一、C++基础

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

#include <iostream>
#include <sstream>
#include <iomanip> // 包含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 四舍五入

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

#include <iostream>
#include <sstream>
#include <iomanip> // 包含std::fixed
#include <cmath> // 包含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.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);
  }
  .
  .
  .
  

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


http://www.kler.cn/news/137042.html

相关文章:

  • 使用 VSCode 通过 Remote-SSH 连接远程服务器详细教程
  • 可编辑38页PPT | 柔性制造企业数字化转型与智能工厂建设方案
  • blender 三种曲线r Curve转化为hair curves
  • js 基础补充3
  • 【Qt】Qt的介绍——Qt的概念、使用Qt Creator新建项目、运行Qt项目、纯代码方式、可视化操作、认识对象模型(对象树)
  • 思科网络设备命令
  • CentOS部署Skywalking
  • 浅谈电动汽车充电桩设计与应用研究
  • Linux 基础操作手记四
  • #gStore-weekly | gBuilder功能详解之表单录入
  • Web之HTML笔记
  • 人工智能基础_机器学习044_使用逻辑回归模型计算逻辑回归概率_以及_逻辑回归代码实现与手动计算概率对比---人工智能工作笔记0084
  • yolo系列模型训练数据集全流程制作方法(附数据增强代码)
  • Git详解及 github使用
  • Spring的声明式事务
  • WebSocket --- ws模块源码解析(详解)
  • webpack配置全局scss
  • Java运行时数据区和常用指令
  • 什么是轻量应用服务器?腾讯云轻量服务器可以干什么?
  • openlayer绘制过程添加提示文字
  • 利用ssh端口转发
  • VsCode连接远程Linux编译环境的便捷处理
  • ​软考-高级-系统架构设计师教程(清华第2版)【第18章 安全架构设计理论与实践(P648~690)-思维导图】​
  • 深度了解LinkedBlockingQueue底层实现原理
  • SpringSecurity+JWT权限认证
  • 2023年中国聚氨酯树脂涂料需求量、市场规模及行业趋势分析[图]