c++ - 显示正确的整数结果

标签 c++ int cout

我有一段代码以这种形式显示结果:示例

  Amount:          10
  Total Amount:    200
  Tax:             30
  Net Balance:     2000

我希望显示结果(例如数学类型)从右侧开始,小数点后有 2 个零 (00)。示例

  Amount:           10.00
  Total Amount:    200.00
  Tax:              30.00
  Net Balance:    2000.00

我正在为此使用 double int,但我真的不知道如何设置从右侧开始的结果量,其中包含一个序列以及一个点和零。

最佳答案

您可以结合 <iomanip> 中的一些设置:

std::cout << std::fixed;   // formatting floating-point numbers
std::cout << std::setprecision(2); // number of floating-point digits
std::cout << std::setw(10);  // width of the whole output string
std::cout << std::right;  // padding to the right

关于c++ - 显示正确的整数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28277074/

相关文章:

c++ - 将 const char 与字符串进行比较

java - 如何循环询问一个数字是否是整数?

c++ - C++库中有没有将字符串转换为算术运算的函数?

c++ - 正在打印指向定义的 int 的成员指针

c++ - 如何计算字符串 vector ?

c++ - 我们如何确保缓存以减少 SQLite 数据库的文件系统写入周期

c++ - 是否可以在 c/c++ 中将 8 个字符(每个 1 字节)存储在 double 类型(8 字节)的变量中?

加号运算符的 C++ 重载

c++ - 为什么此代码会产生引用逗号运算符的警告?

c++ - 共享内存分配 > 2GB(需要链接到 VB6 使用的 32 位 DLL)