温馨提示×

c++中float转string怎么实现

c++
小亿
858
2024-02-28 14:43:15
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C++中,可以使用std::to_string()函数将float类型转换为string类型,示例如下:

#include <iostream>
#include <string>

int main() {
    float number = 3.14f;
    std::string str = std::to_string(number);
    
    std::cout << "Float number: " << number << std::endl;
    std::cout << "String representation: " << str << std::endl;
    
    return 0;
}

运行该程序,输出结果为:

Float number: 3.14
String representation: 3.140000

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:C++中string转double怎么实现

0