在 c++ 中使用 setfill() 以特定格式打印十进制数
Print a decimal number in a particular format using setfill() in c++
我正在尝试打印十进制编号,格式如下:"#####+3.01"情况:有一个小数点 no(在本例中假设为 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
你的代码给了我正确的结果。我正在使用一台 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;
}
相关文章:
- 如何循环打印顶点结构
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 防止主数据类型C++的隐式转换
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 模板参数替换失败,并且未完成隐式转换
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 如何在c++中打印目录
- 复制列表初始化的隐式转换的等级是多少
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 缺少类型说明符,显式类型为"缺少错误"
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 没有显式声明的int[]中的foreach
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 悬挂式C 打印形状来自字符串阵列
- 如何在 c++ 中的字符串中显式打印“”
- 如何打印可流式传输类型的 boost::变体
- 为什么将字符指针流式传输到 cout 不打印地址
- '+'不能添加两个指针,而只是打印一个 int 和一个显式字符串?