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?????
Printable View
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?????
Use the stream member precision().
EDIT: You can find other interesting iostream function members here:Code:std::cout.precision(3);
http://www.cplusplus.com/ref/iostream/ostream/
and
http://www.cppreference.com/cppio/index.html
still not doing it if i do 1.225*1.252245 with 1precision i still not get that precision:sCode: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);
I don't think you understood:
Code:double value = 23.4567;
std::cout.precision(3);
std::cout << value << std::endl; // outputs 23.4
ow , i missread it
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?
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:
EDIT: removed an std:: ... force of habit.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
}