Skip to content
Rain Hu's Workspace
Go back

[C++] Cout functions

Rain Hu

cout Functions

設定顯示小數點位數

#include <iostream>
#include <iomanip>

using namespace std;

int main(){

    double a = 5.43/2.653;
    cout << a << endl;                  // 2.04674
    cout << setprecision(3) << fixed;
    cout << a << endl;                  // 2.047
    
    return 0;
}

顯示 Boolean 值

#include <iostream>

using namespace std;

int main(){

    bool a = true;
    cout << a << endl;          // 1
    cout << std::boolalpha;
    cout << a << endl;          // true

    return 0;
}

Share this post on:

Previous
[C++] 如何產生 random 值
Next
[C++] The C++ Standard Template Library(STL) - Algorithm