如何限制“e”之前的小数位?(How do I limit the decimal places before “e”?)

编程入门 行业动态 更新时间:2024-10-11 01:17:08
如何限制“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.

更多推荐

本文发布于:2023-08-08 01:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数位   limit   places   decimal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!