# rounding off outputs

• 02-02-2005
lastresort
rounding off outputs
I am making this program for a fake store that will take the price of an item and add it to the other items you put in and then multiply by sales tax. I was just wondering how i could round everything off to the 100th. For example, if the price of the item was 1.25 and that was all they bought, 1.25 * .0825 is 1.66562. I would like it to read 1.67. Here is the code so far.

Code:

``` #include <iostream> using namespace std; main() {         const float SALESTAX = .0825;         float totalprice;         float totalpricewithouttax;         float item1;         float item2;         float item3;         float item4;         float item5;         cout<<"Enter the price of an item: ";         cin>> item1;                 if (item1 < 0) {                 cout<<"You must enter a price";                 return 1; }         if (item1 > 0) {                 cout<<"If this is the only item being bought, enter 0.  If not, enter the price for item 2: "; }         cin>> item2;         if (item2 == 0) {                 totalpricewithouttax= item1;                 totalprice= (item1) * (item1 + SALESTAX);         cout<< "The price is "<< totalpricewithouttax<< endl;         cout<< "The total ammount due is "<< totalprice << endl; } }```
• 02-02-2005
Scribbler
Use the fixed and setprecision() manipuators in your cout. Be sure to include <iomanip>.

ie... cout << fixed << setprecision(2) << "The price is.....

fixed will force the output in fixed (vs scientific) format, as well as force the trailing zeros to be displayed. setprecision() sets how many decimal points.
• 02-02-2005
swoopy
> was just wondering how i could round everything off to the 100th.
Code:

```#include <iomanip> . .     cout << "The price is "<< fixed << setprecision(2) << totalpricewithouttax<< endl;```
• 02-02-2005
hk_mp5kpdw
Or without using the <iomanip> header:

Code:

```float fVar; ... cout.precision(2); cout.flags(ios::fixed); cout << fVar;```
• 02-02-2005
Shakti
Edit: damn! beaten.
• 02-21-2005
bugsmashers
Hi,
You can try something like this. It is crude, but effective.

roundedNumber = int (fabs(originalNumber) * 100 + 0.5) / 100