gpt4 book ai didi

c++ - 需要我的输出采用这种形式 XXX.XXX c++

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:52 25 4
gpt4 key购买 nike

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

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

这是我的

C三角形边(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

关于c++ - 需要我的输出采用这种形式 XXX.XXX c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com