![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 38
| decimal formatting Code: double Item::getPrice() {
return (price + (price * 0.07)).digits(2);
}
|
| SterlingM is offline | |
| | #2 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| 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 |
| nadroj is offline | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 38
| Code:
Beverage coke("coke", 3.65, false);
cout<<setprecision(3)<<coke.toString();
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();
}
|
| SterlingM is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| 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 Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #5 |
| Registered User Join Date: Oct 2009
Posts: 38
| 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? |
| SterlingM is offline | |
| | #6 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| 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.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 11-09-2009 at 01:45 AM. |
| laserlight is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| return decimal number back caller function | andy09 | C Programming | 7 | 10-10-2009 08:10 AM |
| hex to binary,hex to decimal | groovy | C Programming | 2 | 01-25-2006 02:14 AM |
| Confused by expression. | Hulag | C Programming | 3 | 04-07-2005 07:52 AM |
| dos game help | kwm32 | Game Programming | 7 | 03-28-2004 06:28 PM |