首页 > 解决方案 > 如何去掉小数点后的这些 0?

问题描述

我的输出应该是这样的:

 n     Recursive   time_r(s)      Iterative   time_i(s)
   1             1    0.000000              1    0.000000
   2             1    0.000000              1    0.000000
   3             2    0.000000              2    0.000000
   4             3    0.000000              3    0.000001
. . .
  43     433494437    2.015535      433494437    0.000002
  44     701408733    3.270357      701408733    0.000002
  45    1134903170    5.258732     1134903170    0.000002

但相反,我得到了这个:

   1        1.0000    0.000000        1.0000    0.000000
   2        1.0000    0.000000        1.0000    0.000000
   3        2.0000    0.000000        2.0000    0.000000
   4        3.0000    0.000000        3.0000    0.000001
. . .
  43433494437.0000    2.015535433494437.0000    0.000002
  44701408733.0000    3.270357701408733.0000    0.000002
  451134903170.0000   5.2587321134903170.0000    0.000002

这是我的代码的样子,我最初让它完美地工作,但后来我被告知要返回一个 std::pair 所以我更改了代码,然后输出改变了。我在不需要零但没有雪茄的代码之前尝试了 std::noshowpoint 。

    std::cout << std::fixed;

    std::cout << "\nFibonacci Output" << std::endl;

    std::cout << std::setw(4) << "n" 
            << std::setw(15) << "Recursive" 
            << std::setw(12) << "time_r(s)" 
            << std::setw(16) 
            << std::setw(15) << "Iterative" 
            << std::setw(12) << "time_i(s)" << std::endl;

    for (int i{ 1 }; i < 44; ++i) {
        std::cout << std::setw(4) << i;
        std::cout << std::setw(15) << time_r(i).first;
        std::cout << std::setw(12) << time_r(i).second;
        std::cout << std::setw(16) << std::setw(15) << time_i(i).first;
        std::cout << std::setw(12) << time_i(i).second << std::endl;
    } 

标签: c++formatoutputdecimaldecimal-point

解决方案


推荐阅读