簡體   English   中英

打印時c ++字符串縮短了兩倍

[英]c++ String is shortening double when printing

此字符串操作可以快速打印出一個雙精度字,但我不知道為什么。 為什么會發生這種情況,如何獲得完整的輸出,如輸出的第一行?

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

這是因為這是默認格式,因此可以使用precision覆蓋它。

它的默認行為是應使用precision以使用固定精度

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

using namespace std;

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

嘗試這個:

using std::fixed;
...
ss.setf(fixed);
ss << myDouble;
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM