decimal formatting

This is a discussion on decimal formatting within the C++ Programming forums, part of the General Programming Boards category; I have this function -- Code: double Item::getPrice() { return (price + (price * 0.07)).digits(2); } First of all, is ...

  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
    20,975
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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 12:45 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21