可以使用固定的setprecision时为什么使用showpoint? [英] Why use showpoint when you can use setprecision fixed?

查看:77
本文介绍了可以使用固定的setprecision时为什么使用showpoint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解showpoint的目的,我知道它会强制显示小数点,但是不使用showpoint而具有"cout<< setprecision<< fixed"就足够了.

I don't quite understand the purpose of showpoint, i know it forces to show a decimal point, but having "cout << setprecision << fixed" is enough without the use of showpoint.

您能告诉我一个必须有showpoint的例子吗?

Can you show me an example where showpoint is a must have?

推荐答案

当覆盖较大范围的值时,可能需要使格式化逻辑在使用定点和科学计数法之间切换,同时仍需要小数点的情况下.如果查看此示例的输出,您会发现它不仅是定点符号:

When covering a large range of values it may be desirable to have the formatting logic to switch between the use of fixed point and scientific notation while still requiring a decimal point. If you look at the output of this example you'll see that it isn't just fixed point notation:

#include <iostream>

int main() {
    for (double value(1e15), divisor(1); divisor < 1e20; divisor *= 10) {
        std::cout << std::noshowpoint << (value / divisor) << '\t'
                  << std::showpoint << (value / divisor)
                  << '\n';
    }
}

这将产生输出

1e+15   1.00000e+15
1e+14   1.00000e+14
1e+13   1.00000e+13
1e+12   1.00000e+12
1e+11   1.00000e+11
1e+10   1.00000e+10
1e+09   1.00000e+09
1e+08   1.00000e+08
1e+07   1.00000e+07
1e+06   1.00000e+06
100000  100000.
10000   10000.0
1000    1000.00
100 100.000
10  10.0000
1   1.00000
0.1 0.100000
0.01    0.0100000
0.001   0.00100000
0.0001  0.000100000

这篇关于可以使用固定的setprecision时为什么使用showpoint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆