# setprecision()

• 11-25-2006
hallo007
setprecision()
if u want to setprecision you need to do it like this:

cout<<setprecision(3)<<..........

is there a way that you only need to say it once so it stay in memory?????
• 11-25-2006
Mario F.
Use the stream member precision().

Code:

`std::cout.precision(3);`
EDIT: You can find other interesting iostream function members here:
http://www.cplusplus.com/ref/iostream/ostream/
and
http://www.cppreference.com/cppio/index.html
• 11-25-2006
hallo007
Code:

```      if(d==0)std::setprecision(0);       if(d==1)std::setprecision(1);       if(d==2)std::setprecision(2);       if(d==3)std::setprecision(3);       if(d==4)std::setprecision(4);       if(d==5)std::setprecision(5);       if(d==6)std::setprecision(6);       if(d==7)std::setprecision(7);       if(d==8)std::setprecision(8);       if(d==9)std::setprecision(6);```
still not doing it if i do 1.225*1.252245 with 1precision i still not get that precision:s
• 11-25-2006
Mario F.
I don't think you understood:

Code:

```double value = 23.4567; std::cout.precision(3); std::cout << value << std::endl; // outputs 23.4```
• 11-25-2006
hallo007

but now it works

but , i want that the setprecision only is true behind the , so:
precision(1)
99.11*1=99.1

how do you that?
• 11-25-2006
Mario F.
The number of digits of an integer can be calculated by adding one to the common logarithm of that number. In C++ the common logarithm is given by log10() defined in <cmath>.

So... one way is:

Code:

```#include <cmath> #include <iostream> using namespace std; int main {     double value = 99.11;     int numdigits = log10(value) + 1;  // returns 2. The number of whole digits in value     int myprecision = numdigits + 1;  // You want one more for one decimal place     cout.precision(myprecision);     cout << value << endl;  // outputs 99.1 }```
EDIT: removed an std:: ... force of habit.