C++ 使用#iostream设置小数点

C++ 使用#iostream设置小数点,c++,C++,我需要让我的cout在小数点后有两位数字,即: 值:2.3将变为:2.30。 我使用iomanip等找到了一些解决方案 但是,我需要一种简单的方法来实现这一点,只需使用iostream centage = (abc/ cdf)*100; cout << "total is "<<abc<<" of "<<cdf<<", or "<<centage <<"%"; centage=(abc/cdf)*100; 不能

我需要让我的
cout
在小数点后有两位数字,即:

值:2.3将变为:2.30。 我使用
iomanip
等找到了一些解决方案

但是,我需要一种简单的方法来实现这一点,只需使用
iostream

centage = (abc/ cdf)*100;
cout << "total is "<<abc<<" of "<<cdf<<", or "<<centage <<"%";
centage=(abc/cdf)*100;
不能只使用:

cout只需使用:


coutstd::setprecision()
操纵器只是方便设置精度和其他输出。您也可以直接在流上设置精度:

std::cout.precision(2);

std::setprecision()
操纵器只是方便设置精度和其他输出。您也可以直接在流上设置精度:

std::cout.precision(2);

在这里,您只使用
iostream
,而不是
cout
使用
printf
,如下所示:

printf("%.2f\n", centage);

在这里,您只使用
iostream
,而不是
cout
使用
printf
,如下所示:

printf("%.2f\n", centage);

不可能复制@erip需要iomanip,而OP似乎真的想让事情变得艰难。@user4581301 Eep!没有看到“没有iomanip”的部分。别担心,@erip。你有正确的解决方案。OP想要的是简单,但却拿出了一些工具,让它变得非常困难。我所能想到的就是一些像这样的糟糕透顶的乱七八糟的东西。@erip可能需要iomanip和OP来复制。@user4581301 Eep!没有看到“没有iomanip”的部分。别担心,@erip。你有正确的解决方案。OP想要的是简单,但却拿出了一些工具,让它变得非常困难。我所能想到的就是像这样可怕的乱七八糟的东西
std::cout