How to use setprecision on vector in C++

2 Answers

0 votes
#include <iostream>
#include <iomanip>
#include <vector>
 
int main() {
    std::vector<double> v = {123.988, -3.14, 51.123499, 0.15, 0.0000134, 200};

    for (const auto &i : v) {
        std::cout << std::setprecision(3) << i << " ";
    }
     
    return 0;
}
  
  
  
  
/*
run:
  
124 -3.14 51.1 0.15 1.34e-05 200 
  
*/

 



answered May 17, 2021 by avibootz
0 votes
#include <iostream>
#include <iomanip>
#include <vector>
 
int main() {
    std::vector<double> v = {123.988, -3.14, 51.123499, 0.15, 0.0000134, 200};

    for (const auto &i : v) {
        std::cout << std::fixed << std::setprecision(3) << i << " ";
    }
     
    return 0;
}
  
  
  
  
/*
run:
  
123.988 -3.140 51.123 0.150 0.000 200.000 
  
*/

 



answered May 17, 2021 by avibootz
...