-1

I'd like to set precision of every element in an array.

Basically I know this format:

cout << fixed << setprecision(3) << num;

but I don't want to print them.

How can I set without std::cout?

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  double arr[3] = {1.2345 , 2.3456 , 3.4567 };
  for( int i = 0; i < 3 ; i++){
    arr[i] = /* setprecision(3) */
  }
  
  return 0;
}
gsztesi
  • 15
  • 2
  • 1
    There is simply no such thing as custom precision of elements in an array. They have `double` type and `double` precision. That's all. – Evg May 27 '22 at 08:54
  • 1
    `stringstream ss; ss << fixed << setprecision(3) << arr[i]; string s = ss.str(); arr[i] = stod(s);` – Eljay May 27 '22 at 12:03

0 Answers0