gpt4 book ai didi

c++字符串在打印时缩短了两倍

转载 作者:搜寻专家 更新时间:2023-10-31 00:18:26 24 4
gpt4 key购买 nike

这个字符串操作以简写方式打印出一个 double 值,我不明白为什么。为什么会出现这种情况,如何才能得到像第一行输出那样的完整输出?

string myString = "The value is ";
ss.str(""); // stringstream from ealier
ss.clear();
ss << myDouble; // Double with value 0.000014577
myString.append(ss.str());
cout << myDouble << endl;
cout << myString << endl;

$ ./myapp
0.000014577
The value is 1.4577e-05

最佳答案

它的默认行为你应该使用精度来使用固定精度

#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
double v = 0.000014577;
cout << fixed << v << endl;
}

关于c++字符串在打印时缩短了两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10608695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com