如何在C ++中将double转换为字符串? [英] How do I convert a double into a string in C++?

查看:317
本文介绍了如何在C ++中将double转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将double作为字符串存储。我知道我可以使用 printf 如果我想显示它,但我只是想把它存储在一个字符串变量,使我可以存储在一个地图后面

I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).

推荐答案

boost(tm):

The boost (tm) way:

std::string str = boost::lexical_cast<std::string>(dbl);

标准C ++ 方式:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

请注意:不要忘记 #include < sstream>

这篇关于如何在C ++中将double转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆