Need to know how to prevent numbers after the hundredths place from showing up

Hello. My program calculates and displays data for a restaurant bill. However, I wanted to how to prevent anything after the hundredths place from being displayed. I tried using setprecision but I am still confused. Maybe I'm using it correctly. I do not want to round the numbers at the hundredths place, I just want to exclude anything after the hundredths place.


Here's how I wanted the data to appear:

Meal Cost: $44.50
Tax Amount: $3.00
Tip Amount: $7.12
Total: $47.50



#include<iostream> //std::cout
#include<iomanip> //std::setprecision

using namespace std;


int main (void)
{

/*****************************

4)Restaurant Bill

******************************/


double mealCharge = 44.50;
double Tax = mealCharge * 0.0675;
double Tip = 0.15 * (mealCharge + Tax);

cout<<"***************** BILL ****************"<<endl;
cout<<setprecision(4)<<endl;
cout<<"Meal Cost: $"<<mealCharge<<endl;
cout<<"Tax Amount: $"<<Tax<<endl;
cout<<"Tip Amount: $"<<Tip<<endl;
cout<<"Total: $"<<(mealCharge + Tax)<<endl;
cout<<endl;
cout<<"****************************************"<<endl;




return 0;
}


/* OUTPUT

***************** BILL ****************

Meal Cost: $44.5
Tax Amount: $3.004
Tip Amount: $7.126
Total: $47.5

****************************************

Process returned 0 (0x0) execution time : 0.202 s
Press any key to continue.


*/
You want cout<<setprecision(2) not four (two decimal places), and you don't need to pass endl after it (it doesn't print anything to the console). To ensure you get trailing zeros (up to the precision amount) do std::cout << std::fixed; before printing the numbers.
You need to use 'fixed' and 'setprecision(2)'.
I needed to include 'fixed'. Thanks!
Topic archived. No new replies allowed.