新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c++中float转string怎么实现,c将float转化为字符串

发布时间:2024-02-28 14:43:15

c++中float转string怎样实现

在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