[C++]将float保留2位小数并转成std::string

#include <iostream>

#include <sstream>

#include <iomanip>

std::string float2string(float value) {

std::ostringstream streamObj;

// Set Fixed -Point Notation

streamObj << std::fixed;

// Set precision to 2 digits

streamObj << std::setprecision(2);

//Add double to stream

streamObj << value;

// Get string from output string stream

return streamObj.str();

}

int main() {

float value = 3.14159;

std::string valueAsString = float2string(value);

std::cout << valueAsString << std::endl; // Prints "3.14"

return 0;

}

相关推荐
美团技术团队15 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
River41616 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥19 小时前
C++ std::set
c++
Fanxt_Ja19 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下20 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶20 小时前
算法 --- 字符串
算法
博笙困了20 小时前
AcWing学习——差分
c++·算法
NAGNIP20 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP20 小时前
大模型微调框架之LLaMA Factory
算法
echoarts20 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust