我有一堆整数,我把它放入stringstream
s。现在我想将stringstream
s更改为string
s,同时保持与string
s的恒定精度。我会怎么做?我知道我可以使用stringstreams.precision()
,但它不工作的一些原因:小数点与std :: stringstream?
float a = 5.23;
float b = 3.134;
float c = 3.0;
std::stringstream ta;
std::stringstream tb;
std::stringstream tc;
ta << a;
tb << b;
tc << c;
ta.precision(2);
tb.precision(2);
tc.precision(2);
std::string out = "";
out += ta.str() + "\n";
out += tb.str() + "\n";
out += tc.str() + "\n";
将返回5.23\n3.134\n3.0
,而不是5.23\n3.13\n3.00
谢谢,但它不工作。即时通讯仍然获得'3.0'而不是'3.00' – noobcpp
@ noobcpp-哎呀!我的错。我只是更新了这一点,提到你需要在字串流上使用'fixed'或'scientific'模式。尝试做出改变,看看它是否修复了一些事情。 – templatetypedef
+1。 @noobcpp:另外,看看:http://www.cplusplus.com/reference/iostream/ios_base/precision/(尽管他们说在一个给我编译器错误的地方使用0)。 –