about setting precision

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// setprecision example
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  double f =3.14159;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  cout << fixed;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  return 0;
}

i copy this code from the tutorial from the forum thee question is what if i only want 1 digit to the right of the decimal point? im' not telling u to make setprecision (2) i want to make it for any number.
Last edited on
That's not how setprecision() works. Precision is the number of digits you know; those before and after the decimal point.
If you read that part of the tutorial more carefully, you'll find the answer to your question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << fixed << setprecision(1);

    cout << .123 << endl;
    cout << 1.23 << endl;
    cout << 12.3 << endl;
    cout << 123. << endl;

    cin.get();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.