Thread: Need help rounding to the hundreth decimal place

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    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?
    Last edited by bardler; 02-27-2008 at 01:43 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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;
    instead of declaring the "salestaxof" variable as double
    Eh -- what? It is a double already.
    Last edited by dwks; 02-27-2008 at 02:05 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I think floor() is part of C++ -- maybe it's just C99.
    floor() is part of standard C++, and can be found in <cmath>.

    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...
    Last edited by laserlight; 02-27-2008 at 01:54 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> 2.375 and i need it to come out to 2.48.

    But that would be a difference of more than 10 cents.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by laserlight View Post
    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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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 . . . .

    [edit] 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]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by dwks View Post
    You mean floor(), of course. This thread is full of typos that could impede communication . . . .

    [edit] 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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You mean floor(), of course.
    Yes, and fixed.

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

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    streams are used to transform float to string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary traslator
    By NiVaG in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2005, 07:01 PM
  2. deciaml place help
    By artgirloc in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 07:47 PM
  3. Replies: 10
    Last Post: 06-12-2002, 03:15 PM
  4. rounding program...s.o.s.
    By pancho in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 10:22 PM
  5. Berlin: Searching for a place to stay
    By fabs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-14-2002, 02:18 AM