# Need help rounding to the hundreth decimal place

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-27-2008
bardler
Need help rounding to the hundreth decimal place
i just started C++ programming and I need to write a program that caculates sales tax and the sales tax comes out to 2.375 and i need it to come out to 2.38.
my program is
Code:

```// ---------------------------------------------------------- #include <iostream> using namespace std; int main() {         // (1) Declare program variables         double wholesaleprice, storemarkupP, salestaxof, storemarkup, sellingprice, total;         const double salestax = 8.25;         // (2) Read in program inputs         cout << "xxxxxxxxxxxxxxxxxxxx\nxEASY BUSINESS CALCx\nxxxxxxxxxxxxxxxxxxxx";         cout << "\nEnter the wholesale price, then press enter\n\$";         cin >> wholesaleprice;         cout << "Enter the markup percentage, then press enter\n&#37;";         cin >> storemarkupP;         // (3) Compute the program outputs         storemarkup = storemarkupP * (.01 * wholesaleprice);         sellingprice = wholesaleprice + storemarkup;         salestaxof = sellingprice * (.01 * salestax);         total = salestaxof + sellingprice;         // (4) Display the results to file or screen         cout << "  Whole sale price: \$" << wholesaleprice << endl;         cout << "Store markup price: \$" << storemarkup << endl;         cout << "    Selling price: \$" << sellingprice << endl;         cout << "        Sales tax: \$" << salestaxof << endl;         cout << "            Total: \$" << total << endl;         return 0; }```
instead of declaring the "salestaxof" variable as double is there a way to declare it so it rounds up to the nearest hundreth decimal place?
• 02-27-2008
dwks
Well, you can either calculate it yourself:
Code:

```double twoplaces(double x) {     return floor(x * 100.0 + 0.5) / 100.0; }```
(I think floor() is part of C++ -- maybe it's just C99. If not, you could cast to unsigned long, as long as your numbers never exceeded that range.)

Or, you could get setprecision from <iomanip> to do it for you. :)
Code:

```#include <iostream> #include <iomanip> std::cout << std::setprecision(100) << salestaxof << std::endl;```
Quote:

instead of declaring the "salestaxof" variable as double
Eh -- what? It is a double already.
• 02-27-2008
laserlight
Quote:

I think floor() is part of C++ -- maybe it's just C99.
floor() is part of standard C++, and can be found in <cmath>.

Quote:

Or, you could get setprecision from <iomanip> to do it for you.
It looks like the OP wants the inaccuracy in an intermediate calculation, so setprecision will not do the trick. It will probably make 2.375 become 2.38 instead. Unless... 2.48 was actually a typo for 2.38...
• 02-27-2008
whiteflags
>> 2.375 and i need it to come out to 2.48.

But that would be a difference of more than 10 cents.
• 02-27-2008
bardler
Quote:

Originally Posted by laserlight
foor() is part of standard C++, and can be found in <cmath>.

It looks like the OP wants the inaccuracy in an intermediate calculation, so setprecision will not do the trick. It will probably make 2.375 become 2.38 instead. Unless... 2.48 was actually a typo for 2.38...

yes it was a typo i ment 2.38
• 02-27-2008
dwks
Quote:

foor() is part of standard C++, and can be found in <cmath>.
You mean floor(), of course. :) This thread is full of typos that could impede communication . . . .

 Another suggestion:
Code:

```        cout << "  Whole sale price: \$" << wholesaleprice << endl;         cout << "Store markup price: \$" << storemarkup << endl;         cout << "    Selling price: \$" << sellingprice << endl;         cout << "        Sales tax: \$" << salestaxof << endl;         cout << "            Total: \$" << total << endl;```
Instead of adding all those spaces manually, iomanip can again do it for you with setw(). Just a thought. [/edit]
• 02-27-2008
bardler
Quote:

Originally Posted by dwks
You mean floor(), of course. :) This thread is full of typos that could impede communication . . . .

 Another suggestion:
Code:

```        cout << "  Whole sale price: \$" << wholesaleprice << endl;         cout << "Store markup price: \$" << storemarkup << endl;         cout << "    Selling price: \$" << sellingprice << endl;         cout << "        Sales tax: \$" << salestaxof << endl;         cout << "            Total: \$" << total << endl;```
Instead of adding all those spaces manually, iomanip can again do it for you with setw(). Just a thought. [/edit]

haha there is alot of things i dont know yet
ive only been doing this for a month, i started class jan 22nd
• 02-27-2008
laserlight
Quote:

You mean floor(), of course.
Yes, and fixed.

Quote:

yes it was a typo i ment 2.38
In that case setprecision() is more appropriate.

Quote:

Instead of adding all those spaces manually, iomanip can again do it for you with setw(). Just a thought.
Having it all align in that way in the code itself is nice too, I suppose.
• 02-27-2008
dwks
It was in the interests of learning more about iomanip. ;) I would probably do it the way the OP has it myself.

And my formula was wrong. I'd better edit it. Sigh. :)
• 02-27-2008
matsp
I'd say that setprecision is the RIGHT solution here. Using formulas or such can backfire - suppose the result is exactly 0.7 [in "perfect math"] - so the computer will store that as 0.6999999999999999 [as many 9's as is required to fill up all the bits of a floating point number]. Using setprecision(2), you will get 0.70 - using any other method may end up with 0.69999999 or 0.7 - the latter missing the zero to make it line up correctly.

--
Mats
• 02-27-2008
Elysia
The only problem is that you only learn how to manipulate streams and not gain any valuable experience of how to actually deal with this problem in other situations such as when writing a GUI.
• 02-27-2008
matsp
Quote:

Originally Posted by Elysia
The only problem is that you only learn how to manipulate streams and not gain any valuable experience of how to actually deal with this problem in other situations such as when writing a GUI.

The situation needs to be dealt with in the same way when it comes to GUI - the exact method may be different, but the rounding needs to be done when translating to a string from a float value, not by manipulating the number itself.

--
Mats
• 02-27-2008
Elysia
Then the method to translate the float into a string while rounding should be preferred to be taught instead on relying on streams to perform the conversion instead. That's my thoughts on the matter.
• 02-27-2008
vart
streams are used to transform float to string
• 02-27-2008
matsp
Quote:

Originally Posted by vart
streams are used to transform float to string

That is ONE of several ways, but yes, "stringstream" is what I would use in such a case [in fact, I recently used that to do integer to string conversion in a Windows app I'm working on (very slowly) at home]. And if that's what you want to use, then setprecision works perfectly fine.

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last