23

如何在 C++ 中格式化浮点数以输出到四舍五入的两位小数?我没有运气,setw因为setprecision我的编译器只是告诉我它们是not defined.

cout << "Total : " << setw(2) << total << endl;

总产出:Total : 12.3961

我希望它是:12.40或者12.39如果它的工作量太大而无法汇总。

4

7 回答 7

19

您需要包含<iomanip>并提供命名空间范围setw and setprecision

#include <iomanip>
std::setw(2)
std::setprecision(5)

尝试:

cout.precision(5);
cout << "Total : " << setw(4)   << floor(total*100)/100 << endl;

或者

 cout << "Total : " << setw(4)   << ceil(total*10)/10 << endl;

iostream 提供了精度功能,但要使用 setw,您可能需要包含额外的头文件。

于 2013-01-21T03:23:00.377 回答
19

使用cout << fixedorcout.setf(ios::fixed)std::cout.precision(<# of decimal digits>),如下所示(使用 OSX Mavericks 附带的 Clang-503.0.40 编译器):

#include <iostream>

int main()
{
   using namespace std;

   float loge = 2.718;
   double fake = 1234567.818;
   cout << fixed;
   cout.precision(2);
   cout << "loge(2) = " << loge << endl;
   cout << "fake(2) = " << fake << endl;
   cout.precision(3);
   cout << "loge(3) = " << loge << endl;
   cout << "fake(3) = " << fake << endl;
}

输出是(注意四舍五入):

loge(2) = 2.72
fake(2) = 1234567.82
loge(3) = 2.718
fake(3) = 1234567.818

这是简单的版本。代替 using cout << fixed;,您可以使用cout.setf(ios::fixed);(用于显示科学记数法,将fixed替换为Scientific;两者都将设置小数点右侧的位数)。请注意,如果格式标志不包括fixedscientific,则 cout.precision() 还用于设置小数点两侧总显示的位数。网上有这方面的教程。

于 2014-08-05T22:58:47.450 回答
13

要还包括尾随零,仅设置精度是不够的。您还必须将浮点格式更改为固定格式,它使用所告知的位数作为setprecision小数点后的位数

std::cout << std::fixed << std::setprecision(2) << v;

在线工作示例代码

于 2013-01-21T04:11:49.800 回答
6

如果你想从四舍五入中得到尾随零,你可以使用 C 函数printf

#include <iostream>
#include <cstdio>

int main() {
    float v = 12.3961;
    std::printf("%.2f",v); //prints 12.40
}

相比:

#include <iostream>
#include <iomanip>

int main() {
    float v = 12.3961;
    std::cout << std::setprecision(4) << v; //prints 12.4
}
于 2013-01-21T03:29:17.927 回答
3

你可以用 C++20 做到这一点std::format

std::cout << std::format("Total     : {:.2f}\n", total);

{fmt} 库fmt::format中的函数,基于。{fmt} 还提供了整合格式化和输出的功能,使它更容易和更高效(godbolt):std::formatprint

#include <fmt/core.h>

int main() {
  fmt::print("Total     : {:.2f}\n", 12.3961);
}

输出:

Total     : 12.40

这使用 IEEE754 默认舍入模式(四舍五入到最接近的偶数),因此您必须自己四舍五入(如何向上/向下舍入浮点值的小数位?C++)。

于 2021-05-27T16:12:52.670 回答
2

这个有点脏。。。

template <typename T>
string sFormat(const char *f, T value)
{
    char buffer[64];
    snprintf(buffer, sizeof(buffer), f, value);
    return string(buffer);
}

并做这样的事情:

cout << "Result :" << sFormat(" %5.2f", 3.14) << sFormat(" %5d", 2300) << endl;
于 2020-09-13T21:47:21.647 回答
0

当我在 Mac 上使用一些 C++ 时,只是偶然发现了这个问题 ..(Xcode 在 Intel 上安装 OSX 11.5.2)

这是我发现总结上面提供的内容并显示我的结果。

我发现令人惊讶的是,“cout << fixed”会杀死舍入和最后一位数字,从而导致您在下面看到的变化:

#include <iostream>
using namespace std;

int main() {
    // insert code here...
    
    int yearInt = 1972;
    cout << "yearInt = " << 1972 << "\n";

    double myDouble = 2.123456789123456789;
    cout << "myDouble = " << myDouble << "\n";
    
    cout.precision(2);
    cout << "Setting cout.precision(2)\n";
    cout << "myDouble = " << myDouble << "\n";
    cout.precision(3);
    cout << "Setting cout.precision(3)\n";
    cout << "myDouble = " << myDouble << "\n";
    cout.precision(4);
    cout << "Setting cout.precision(4)\n";
    cout << "myDouble = " << myDouble << "\n";
    cout.precision(9);
    cout << "Setting cout.precision(9)\n";
    cout << "myDouble = " << myDouble << "\n";
    cout.precision(14);
    cout << "Setting cout.precision(14)\n";
    cout << "myDouble = " << myDouble << "\n";
    cout << fixed;
    cout.precision(14);
    cout << "Setting cout.fixed & cout.precision(14)\n";
    cout << "myDouble = " << myDouble << "\n";
    
    return 0;
}

给出:

yearInt = 1972                   // as expected
myDouble = 2.12346               // default showing 5 decimal places displayed, but you can see it's rounded up also
Setting cout.precision(2)
myDouble = 2.1                   // should be 2, but it's showing 1, rounded down from .12
Setting cout.precision(3)
myDouble = 2.12                  // should be 3, but it's showing 2, rounded down from .123
Setting cout.precision(4)
myDouble = 2.123                 // should be 4, but it's showing 3, rounded down from .1234
Setting cout.precision(9)
myDouble = 2.12345679            // should be 9, but it's showing 8, rounded up from .123456789
Setting cout.precision(14)
myDouble = 2.1234567891235       // should be 14, but it's showing 13, rounded up from . .. 2345
Setting cout.fixed & cout.precision(14)
myDouble = 2.12345678912346      // should be 14 places and it is, but still rounding up
Program ended with exit code: 0
于 2021-08-31T11:01:05.370 回答