C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2009, 05:34 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 38
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..
SterlingM is offline   Reply With Quote
Old 11-08-2009, 10:26 PM   #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   Reply With Quote
Old 11-09-2009, 01:13 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 38
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();
}
SterlingM is offline   Reply With Quote
Old 11-09-2009, 01:18 AM   #4
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 11-09-2009, 01:33 AM   #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   Reply With Quote
Old 11-09-2009, 01:42 AM   #6
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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