【问题标题】:Print a decimal number in a particular format using setfill() in c++使用 c++ 中的 setfill() 以特定格式打印十进制数
【发布时间】:2017-11-18 07:23:40
【问题描述】:

我正在尝试打印十进制数字。采用以下格式:“#####+3.01” 案例:有一个十进制数(在这种情况下假设为 3.01)。我必须用它的符号 +/- 前面加上 y 来打印它。的#,有一些固定的总宽度。 (假设在这种情况下 x = 10)。

我试着做这样的事情:

   double no = 3.01;
   cout << setfill('#') << setw(10) ;
   cout << setiosflags(ios::showpos);
   cout << fixed << setprecision(2) << no << endl;

但我得到以下输出:

    +#####3.01

预期输出:

    #####+3.01

【问题讨论】:

标签: c++


【解决方案1】:

您的代码给了我正确的结果。我正在使用 Linux 机器。

万一它是一个依赖于操作系统的问题,试试这个代码:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double no = 3.01;
    cout << setfill('#') << std::right<< setw(10) ;
    cout << setiosflags(ios::showpos);
    cout << fixed << setprecision(2) << no << endl;
}

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 2014-03-09
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多