如何使如何製作「std :: cout << 123456789.12」print「123456789.12」?
std::cout << 123456789.12
打印:
123456789.12
它總是打印此:
1.23457e+008
我知道我有標誌打球,但我不能很找出正確的組合。如果我設置了固定的標誌,它打印
123456789.120000
如何使如何製作「std :: cout << 123456789.12」print「123456789.12」?
std::cout << 123456789.12
打印:
123456789.12
它總是打印此:
1.23457e+008
我知道我有標誌打球,但我不能很找出正確的組合。如果我設置了固定的標誌,它打印
123456789.120000
您可以使用:
#include <iostream>
#include <limits>
using namespace std;
int main() {
double c = 123456789.12;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << c << endl;
return 0;
}
基本上limits
package對所有構建特質類型。 浮點數(float/double/long double
)的一個特徵是digits10
attribute。這個定義基礎浮點數的精度10
要看下去,看看另一個類似的問題:How do I print a double value with full precision using cout?
請注意,從'+ 1'得到的最後一位數字不能作爲有用的信息,除非能夠準確地序列化和恢復值。 – Potatoswatter
如何...?
一種方法: -
#include <iostream>
#include <iomanip>
int main() {
double f =123456789.12;
std::cout << std::fixed << std::setprecision(2) << f << '\n';
return 0;
}
見here
請尋找適當的引用
您可以使用boost :: lexical_cast的如下:
#include <boost/lexical_cast.hpp>
std::cout << boost::lexical_cast<std::string>(123456789.12);
更多信息可以在http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html
找到
std :: setprecision:http://en.cppreference.com/w/cpp/io/manip/setprecision – willll
???新的CPP程序員爲什麼添加STL標誌? 反正你可以通過'std :: setprecision(int)設置精度' 嘗試'std :: cout << std :: setprecission(20)<< 123456789.12 << endl;' – DOOM
請參閱http://stackoverflow.com/questions/22177656/convert-double-to-string-with-fixed-point-notation-no-trailing-zeroes-and-witou –