如何限制“e”之前的小数位?(How do I limit the decimal places before “e”?)
我想在格式为“1.00e6”的C ++中打印一个浮点变量x,其中我只想在e之前的小数点后2位。
阅读本文: http : //www.cplusplus.com/reference/cstdio/printf/ ,我不确定使用哪个说明符。 这可以用C ++完成吗?
I want to print a float variable x in C++ in the format "1.00e6", where I only want 2 places after the decimal point before the e.
Reading over this: http://www.cplusplus.com/reference/cstdio/printf/, I'm not sure which specifier to use. Can this be done in C++?
最满意答案
您可以在C ++中执行相同的操作:
double f = 1.00e6; std::cout.precision(2); std::cout << std::scientific; std::cout<<f <<std::endl; It will output: 1.00e+06 in this case.编辑 :正如@ user657267所指出的,还有一个precision的操纵器,
double f = 1.00e6; std::cout << std::setprecision(2) << std::scientific << f << '\n';应该有同样的效果。
You can do the same in C++:
double f = 1.00e6; std::cout.precision(2); std::cout << std::scientific; std::cout<<f <<std::endl; It will output: 1.00e+06 in this case.EDIT: as pointed out by @user657267, There's a manipulator for precision as well,
double f = 1.00e6; std::cout << std::setprecision(2) << std::scientific << f << '\n';should have same effect.
更多推荐
发布评论