Thursday, July 30, 2009

How Do I Display Only The Last 2 Digits after the Decimal Point In C++?

How Do I Display Only The Last 2 Digits after the Decimal Point In C++ ?





Example Of What I'm Working On.





double a=19.99;


double b=0.08123;


double c=a*b;


double d=c+a;





cout %26lt;%26lt; "The Total Of Both Numbers : ";


cout %26lt;%26lt; d;


cout %26lt;%26lt; endl;





system("pause");


system("cls");

How Do I Display Only The Last 2 Digits after the Decimal Point In C++?
Rain is right use the cout function. but you also need to fix your output just incase you get only one decimal.





That would happen if you get like 5.3 and you want it to show 5.30. Don't put in the ios fixed if you don't mind showing 5.3.





here is another example:





#include %26lt;iomanip%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main()


{


double a = 440;


double b = 387.3532;





cout.setf(ios::fixed);


cout%26lt;%26lt; setprecision(2) %26lt;%26lt; a %26lt;%26lt;'\n';


cout%26lt;%26lt; setprecision(2) %26lt;%26lt; b %26lt;%26lt;endl;


}
Reply:wth is this
Reply:#include %26lt;iomanip%26gt;





using namespace std;





cout %26lt;%26lt; setprecision(2) %26lt;%26lt; d;
Reply:c = a x b


c = 19.99 x 0.08123


= 1.6237877 or 1.62





d = c + a


= 1.62 + 19.99


= 21.61


No comments:

Post a Comment