How to display two decimals places in the final result?

How do I make the final result display two decimal places? (since I am working with money.) and if there is no decimal I need .00

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


int main()
{
setprecision(2);
system("color f0");
ifstream inputfile;

//Declare variables
string Fname = " ";
string Lname = " ";
string ssn = " ";
int count = 0;
int hours = 0;
double overtime = 0.00;
double rate = 0.00;
double gross = 0.00;
double net = 0.00;
double deduct = 0.00;
inputfile.open("Payroll.txt");
inputfile >> Fname >> Lname >> ssn >> hours >> rate;


cout << "\t\t************************************" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t************************************" << endl;

//Header
cout << "SSN \tName \tHours \tRate \tGross \tDeductions \tNetPay" << endl;
cout << "___ \t____ \t____\t____ \t_____ \t____ \t\t______" << endl;

//Checking File
while (inputfile.eof() == false)
{
count++;

//Math
if (hours > 40)
{
overtime = (40 * rate) + (hours - 40) * rate * 1.5;
gross = overtime;
deduct = gross * 0.1;
net = gross - deduct;

}
else

gross = hours * rate;
deduct = gross * 0.1;
net = gross - deduct;

//edit name



//display

cout << " " << ssn.substr(7) << " \t" << " " << Fname << "\t" << " " << hours << "\t" << " "
<< rate << "\t" << " " << gross << "\t" << " " << deduct << "\t\t" << " " << net << endl;
inputfile >> Fname >> Lname >> ssn >> hours >> rate;

}
cout << endl;
cout << "Numbers of Records: " << count << endl;
cout << "\n T H A N K Y O U\n" << endl;
system("pause");
return 0;
}
Last edited on
Don't double post: http://cplusplus.com/forum/general/269977/

And don't change your previous title to something different after you already had someone reply to your question. Now it's just confusing.

Use code tags. Edit your post and add:
[code]
    { your code here }
[/code]


To always show two decimal places, used fixed and setprecision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <iomanip>

int main()
{
    using namespace std;
    double price;
    
    price = 18.65;
    cout << fixed << setprecision(2) << price << '\n';
    
    price = 18.0;
    cout << fixed << setprecision(2) << price << '\n';
}
Last edited on
Topic archived. No new replies allowed.