How to use setprecision in C++

前端 未结 7 1586
遇见更好的自我
遇见更好的自我 2020-11-27 05:38

I am new in C++ , i just want to output my point number up to 2 digits. just like if number is 3.444, then the output should be 3.44 o

7条回答
  •  青春惊慌失措
    2020-11-27 06:35

    #include 
    #include 
    using namespace std;
    

    You can enter the line using namespace std; for your convenience. Otherwise, you'll have to explicitly add std:: every time you wish to use cout, fixed, showpoint, setprecision(2) and endl

    int main()
    {
        double num1 = 3.12345678;
        cout << fixed << showpoint;
        cout << setprecision(2);
        cout << num1 << endl;
    return 0;
    }
    

提交回复
热议问题