cout Functions

設定顯示小數點位數

  • setprecision(int n) and fixed
#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 值

  • std::boolalpha
#include <iostream>

using namespace std;

int main(){

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

    return 0;
}