将浮点数格式化为没有尾随零的字符串的推荐方法是什么?to_string()返回"1.350000",sprintf也是如此。我不想要固定数量的小数..。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = to_string(1.35);
cout << s << endl;
}发布于 2015-04-22 20:31:24
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
stringstream ss;
ss << 1.35;
cout << ss.str();
return 0;
}发布于 2015-04-22 20:28:15
std::to_string & sprintf没有赋予您任何控制在将浮点型转换为字符串时得到的尾随零的数量的能力。尝试使用std::stringstream,您将拥有控制尾随零所需的所有选项。
https://stackoverflow.com/questions/29797515
复制相似问题