Thread: decimal formatting

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    decimal formatting

    I have this function --
    Code:
    double Item::getPrice() {
    	return (price + (price * 0.07)).digits(2);
    }
    First of all, is digits(int) the correct way to format decimals (I only want two digits to the right of the decimal)? The compiler says I need a struct/class/union to the left of digits(). I'm not sure what I would use for that..

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    No. Did someone suggest you do it this way?

    Also, the computer doesnt care how many decimal places there are, either should you. However, you only really need to worry about this sort of thing when you are doing output, ex "cout" to the screen. So this function should just return the result, then whatever function is responsible for the output should worry about how it should be printed. Check this link out, which has examples at the bottom: setprecision - C++ Reference

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Code:
    	Beverage coke("coke", 3.65, false);
    	cout<<setprecision(3)<<coke.toString();
    No matter what I change the setprecision parameter to, it always displays 3.9055. Is it because I am calling a function? toString() looks like this.

    Code:
    string Beverage::toString() {
    	ostringstream result;
    	if(isAlcoholic == true)
    		result<<getName()<<" is an alcoholic beverage."<<std::endl;
    	else
    		result<<getName()<<" is an non-alcoholic beverage."<<std::endl;
    	result<<"It's price is "<<getPrice()<<"."<<std::endl;
    	return result.str();
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use setprecision within the toString() member function. By the way, have you considered overloading operator<< for std::ostream instead of writing a toString() member function?
    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

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Okay I got that. If I wanted it in "##.##" form, what can I use to do that? Does precision handle that? Or is there some other way to do it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use std::setw with std::setfill. Frankly though, this kind of formatting is not one of the strong points of C++'s output streams; C's formatted output functionality probably does this better. You can get similiar printf-like functionality for C++ output streams via Boost.Format.
    Last edited by laserlight; 11-09-2009 at 01:45 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 08:10 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM