设置精度C ++

时间:2017-12-04 02:27:19

标签: c++ output precision setw

所以,我试图在我的代码中设置输入值的精度。我希望在精确度之后用两个小数点打印值,虽然我不确定如何。

这是我的代码。

#include <iostream>
#include <iomanip>

float uphill, wellD, waterLvl, buckVol;
float buckAscRate, downHill, volume;
float last;
float timeReq;

int scene = 1;





void timeRequired()
{
    std::setw(2);
    std::setprecision(2);

    std::cout << "Scenario " << scene << ":" << std::endl;
    std::cout << "up hill" << "          " << uphill << " sec" << std::endl;
    std::cout << "well diamter" << "          " << wellD << " in" << std::endl;
    std::cout << "water level" << "          " << waterLvl << " in" << std::endl;
    std::cout << "bucket volume" << "          " << buckVol << " cu ft" << std::endl;
    std::cout << "bucket ascent rate" << "          " << buckAscRate << " in/sec" << std::endl;
    std::cout << "down hill" << "          " << downHill << " sec" << std::endl;
    std::cout << "required volume" << "          " << volume << " cu ft" << std::endl;

    timeReq = (uphill + downHill);

    std::cout << "TIME REQUIRED" << "          " << timeReq << " sec" << std::endl;

    std::cout << " " << std::endl;



}

void scenarioCONT()
{
    do
    {
        std::cin >> wellD;
        std::cin >> waterLvl;
        std::cin >> buckVol;
        std::cin >> buckAscRate;
        std::cin >> downHill;
        std::cin >> volume;
        std::cin >> last;

        if (uphill <= 1) uphill = 2;
        if (wellD <= 0) wellD = 1;
        if (waterLvl <= 0) waterLvl = 1;
        if (buckVol <= 0) buckVol = 1;
        if (buckAscRate <= 0) buckAscRate = 1;
        if (downHill <= 0) buckAscRate = 1;
        if (volume <= 0) volume = 1;

        if (last > 1)
        {
            uphill = last;
            scenarioCONT();

        }



    } while (last != 0);



}
void scenario()
{
    do
    {
        std::cin >> uphill;
        std::cin >> wellD;
        std::cin >> waterLvl;
        std::cin >> buckVol;
        std::cin >> buckAscRate;
        std::cin >> downHill;
        std::cin >> volume;
        std::cin >> last;

        if (uphill <= 1) uphill = 2;
        if (wellD <= 0) wellD = 1;
        if (waterLvl <= 0) waterLvl = 1;
        if (buckVol <= 0) buckVol = 1;
        if (buckAscRate <= 0) buckAscRate = 1;
        if (downHill <= 0) buckAscRate = 1;
        if (volume <= 0) volume = 1;

        if (last > 1)
        {
            timeRequired();
            uphill = last;
            scenarioCONT();

        }
            scene++;
            timeRequired();


    } while (last != 0);


}





int main()
{

    scenario();

    system("pause");
}

我被告知使用ionmanip来设定精确度,但我并不是100%就如何做到这一点。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以使用std::setprecision功能。以下示例直接取自http://www.cplusplus.com/reference/iomanip/setprecision/

  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';

答案 1 :(得分:0)

如果您想输出2位小数,则应使用std::fixedstd::setprecision()

看看here

为了便于理解,这里有一个例子: cout << setprecision(2)<< 3.1415;输出3.1(总共2位数)和

cout << setprecision(2)<<fixed<< 3.1415;输出3.14(浮点后2位数)