例如我得到一个三角形的6的面积,我需要我的输出显示006.000

我知道setprecision可以将小数位限制为三,但是如何在解决方案前放置零?

这就是我所拥有的

CTriangle Sides(3,4,5);

cout << std::fixed << std::setprecision(3);

cout << "\n\n\t\tThe perimeter of this tringle is   = " << Sides.Perimeter();

cout << "\n\n\t\tThe area of the triangle is        = " << Sides.Area();

cout << "\n\n\t\tThe angle one is                   = " << Sides.Angleone();

cout << "\n\n\t\tThe angle two is                   = " << Sides.Angletwo();

cout << "\n\n\t\tThe angle three is                 = " << Sides.Anglethree();

cout << "\n\n\t\tThe altitude one of the triangle   = " << Sides.Altitudeone();

cout << "\n\n\t\tThe altitude two of the triangle   = " << Sides.Altitudetwo();

cout << "\n\n\t\tThe altitude three of the triangle = " << Sides.Altitudethree();

输出是
            The perimeter of this triangle is   = 12.000

            The area of the triangle is        = 6.000

            The angle one is                   = 90.000

            The angle two is                   = 36.870

            The angle three is                 = 53.130

            The altitude one of the triangle   = 2.400

            The altitude two of the triangle   = 6.667

            The altitude three of the triangle = 3.750

但无论我的解决方案是什么,我都需要所有答案都采用XXX.XXX格式。(因为值会发生变化)

任何帮助表示赞赏,谢谢!

最佳答案

使用可以使用paddingfilling机械手:

std::setfill('0');             // is persistent
//...

cout << std::setw(7) << value; // required for each output

10-04 12:45
查看更多