Code::Blocks printf double 在 Windows 中具有固定精度

Code::Blocks printf double with fixed precision in Windows

我在两个不同的操作系统上构建并运行以下代码:

#include <bits/stdc++.h>
using namespace std;

int main(){

    double d=1.123456;
    printf("%.5lf ",d);
    cout<<fixed<<setprecision(5)<<d;

    return 0;
}

Code::Blocks in Ubuntu 输出:1.12346 1.12346(如预期)

Code::Blocks in Windows 输出:0.00000 1.12346(为什么 0.00000 !!!)

对于 printf 调用,您表明您正在传递一个长双精度数(使用 'lf')但只传递一个双精度数,它在 ubuntu 上起作用的事实是一个意外而不是一个意外表明它是正确的。

如果您使用 %.5f 而不是 %.5lf,那么这两种情况都应该有效。