How to round floating point number to 2 decimals in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <iomanip>

int main() {
    std::vector<double> v = {123.988, -3.14, 51.123499, 0.15, 0.0000134, 200};

    for (auto &i : v) {
        std::cout << std::setprecision(2) << std::fixed << i << " ";
    }
    
    return 0;
}




/*
run:

123.99 -3.14 51.12 0.15 0.00 200.00 

*/

 



answered May 17, 2021 by avibootz
...