Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding

Status
Not open for further replies.

mgbeye

Programmer
May 30, 2001
47
US
My program figures the payroll details for two employees and then calculates a total. My problem is in rounding. My results are off by a penny. I need to know how to round a variable so that I can use it in an addition statment. Here is my code.

/* Lab 03P2.cpp
Name: Melissa Beyer
Date: 09/09/01
Purpose: Calculate Payroll
Input: Hours worked and Hourly rate for two employees.
Output: Employee number, Hours worked, hourly rate, gross pay, tax, Net pay. */

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const double TaxRate = .2; //Tax rate as decimal 0.2 = 20%.
double Worked1, PayRate1, Gross1, Tax1, Net1;
double Worked2, PayRate2, Gross2, Tax2, Net2;
double WorkedTot, GrossTot, TaxTot, NetTot;

cout << &quot;CPT 267 Lab 03P2 - Melissa Beyer&quot; << endl;
cout << &quot;Program to calculate payroll.&quot; << endl << endl;

//Get info for Employee #1.
cout << &quot;Enter hourly rate for Employee #1: &quot;;
cin >> PayRate1;
cout << endl;
cout << &quot;Enter number of hours worked for Employee #1: &quot;;
cin >> Worked1;
cout << endl << endl;

//Get info for Employee #2.
cout << &quot;Enter hourly rate for Employee #2: &quot;;
cin >> PayRate2;
cout << endl;
cout << &quot;Enter number of hours worked for Employee #2: &quot;;
cin >> Worked2;
cout << endl << endl;

//Calculations for Employee #1.
Gross1 = PayRate1 * Worked1;
Tax1 = Gross1 * TaxRate;
Net1 = Gross1 - Tax1;

//Calculations for Employee #2.
Gross2 = PayRate2 * Worked2;
Tax2 = Gross2 * TaxRate;
Net2 = Gross2- Tax2;

//Totals
WorkedTot = Worked1 + Worked2;
GrossTot = Gross1 + Gross2;
TaxTot = Tax1 + Tax2;
NetTot = Net1 + Net2;

//Output.
cout << fixed << setprecision(2);
cout << setw(8) << &quot;EMP&quot;
<< setw(8) << &quot;Hours&quot;
<< setw(8) << &quot;Rate&quot;
<< setw(8) << &quot;Gross&quot;
<< setw(8) << &quot;Tax&quot;
<< setw(8) << &quot;Net&quot;
<< endl;
cout << setw(8) << &quot;1&quot;
<< setw(8) << Worked1
<< setw(8) << PayRate1
<< setw(8) << Gross1
<< setw(8) << Tax1
<< setw(8) << Net1
<< endl;
cout << setw(8) << &quot;2&quot;
<< setw(8) << Worked2
<< setw(8) << PayRate2
<< setw(8) << Gross2
<< setw(8) << Tax2
<< setw(8) << Net2
<< endl;
cout << setw(8) << &quot;&quot;
<< setw(8) << &quot;---&quot;
<< setw(8) << &quot;&quot;
<< setw(8) << &quot;------&quot;
<< setw(8) << &quot;------&quot;
<< setw(8) << &quot;------&quot;
<< endl;
cout << setw(8) << &quot;Totals&quot;
<< setw(8) << WorkedTot
<< setw(8) << &quot;&quot;
<< setw(8) << GrossTot
<< setw(8) << TaxTot
<< setw(8) << NetTot
<< endl << endl;

return 0;

}

My totals for TaxTot and NetTot are both off by a penny. Does anyone have any suggestions??
 
do something like this:
int round(int a, int rndrate)
{
int rc = powl(10,rndrate);
return (a - a%rc + rc*(( (a%rc)>=(rc>>1) )?1:0))
}

and you will round as:

int x = round(123456,2);//rounding last two from 123456 Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top