2017-11-18 63 views
0

我试图打印一个十进制数字。格式如下:“##### + 3.01” 案例:有一个小数否(在这种情况下说3.01)。我必须打印它的符号+/-在y之前。 #,有一些修正总宽度。 (在这种情况下,假设x = 10)。在C++中使用setfill()打印特定格式的十进制数字

我试图做这样的事情:

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

但我正在逐渐followinfg输出:

+#####3.01 

预期输出:

#####+3.01 
+2

的可能的复制[?的std :: ostringstream把标志在错误的位置(https://stackoverflow.com/questions/ 46729847/stdostringstream-puts-sign-in-wrong-location) – Barmar

回答

0

您的代码给了我正确的结果。我正在使用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; 
} 
+0

它工作。谢谢。 –