简体   繁体   English

设置精度C ++

[英]Setting up precision C++

So, i'm attempting to set the precision for input values in my code. 因此,我正在尝试为代码中的输入值设置精度。 I want the values to be printed with two decimal points afterwards of precision though i'm not exactly sure how. 我不希望精确地将数值打印后精确到两位小数。

Here's my code. 这是我的代码。

#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");
}

I've been told to use ionmanip to set the precision, though i'm not 100% on how to do it. 有人告诉我使用ionmanip来设置精度,尽管我不是100%做到这一点。 Any suggestions? 有什么建议么?

You can use std::setprecision function. 您可以使用std::setprecision函数。 The below example is directly taken from http://www.cplusplus.com/reference/iomanip/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';

If you want to output 2 decimal digits, you should use std::fixed , together with std::setprecision() . 如果要输出2个十进制数字,则应使用std::fixed以及std::setprecision()

Take a look here . 在这里看看。

For easier understanding, here is an example: cout << setprecision(2)<< 3.1415; 为了更容易理解,下面是一个示例: cout << setprecision(2)<< 3.1415; outputs 3.1 (2 digits in total) and 输出3.1 (总共2位数字)和

cout << setprecision(2)<<fixed<< 3.1415; outputs 3.14 (2 digits after floating point) 输出3.14 (浮点数后2位)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM