# print floating-points

• 12-01-2002
lambs4
print floating-points
I have copied the below example into my compiler from a book. The example output it shows is:
Quote:

How many departments are there in the company? 7
How many employees are in the company? 45
What is the company's total payroll? 7645.32

The average number of employees per department is 6
The average payroll per department is \$1092.19
When I compile the program it gives me scientific notation for the average payroll per department instead of 1029.19. Why's that?
I'm using the Borland C++ 4.5 complier

Code:

```// Filename: DIV.CPP #include <iostream.h> #include <iomanip.h> int main() {   int numDept, totalEmp, avgDept;   float avgPay, pay;   cout <<"How many departments are there in the company? ";   cin >> numDept;   cout <<"How many employees are there in the company? ";   cin >> totalEmp;   cout <<"What is the company's total payroll? ";   cin >> pay;   avgDept = totalEmp / numDept;  // Integer division   avgPay = pay / numDept;  // Floating-point division;   cout <<"\nThe average number of per department employees "         "is " << avgDept << "\n";   cout.setf(ios::showpoint);   cout <<"The average payroll per department is \$"                   << setprecision(2) << avgPay;   return 0; }```
• 12-01-2002
skipper
You also need cout << fixed;. 'fixed' allows decimals to be displayed rather than scientific notation.

-Skipper

P.S. setprecision(2) will give you a displayed value of 1.2E+03 for the value of '1200'. It's the '1.2' that has had its "precision" set to '2'. Had setprecision() had an argument of '3', you would display 1.20E+03 instead.