如何使如何制作“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 –