|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
Join Date: Sep 2006
Location: NYC
Posts: 272
|
Trying to set Decimals in C++
I want to set the precision of the variable total_price to only 2 decimal places (EX: 2.82)
Here's the code: #include #include //Price It //3-13-07 double original_price (void); double discount_price (void); double calculate_price (double, double); int main () { double price; double discount; double total_price; double tax; for(int x=1; x<6; x++) { price = original_price(); discount = discount_price(); total_price = calculate_price(price,discount); cout< cout< cout< cout< } return 0; } double original_price (void) { double price; cout<<"What is the original price of the item?"< cout< } double discount_price (void) { double discount; cout<<"What is the size of the discount? (in decimal value)"< cout< } double calculate_price (double price, double discount) { double total_price; double tax; tax = 0.06; price = price - price * discount; total_price = price * tax + price; return total_price; } |
|
|
|
|
|
#2 |
|
Member (11 bit)
|
Before that line just put.
cout.precision( 2 ); cout.setf( ios::fixed ); cout.setf( ios::showpoint ); You also need to #include < iomanip > (sorry for the spaces, the forum makes it not be able to be seen without them) You should just be able to put the "cout.precision (2);" but i've been taught toput the other stuff to. showpoint is used to show something like 22.00, where it will still display the ".00" and fixed just sets it to a fixed point notation. Last edited by FLG; 03-14-2007 at 10:17 AM. |
|
|
|
|
|
#3 | |
|
Member (9 bit)
Join Date: Sep 2006
Location: NYC
Posts: 272
|
Quote:
Anyway, thanks. That worked. Thanks so much !
|
|
|
|
|
|
|
#4 |
|
Professional gadfly
|
If you wrap your C++ code with PHP code tags it will display everything properly.
PHP Code:
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Spyware help | Chad44 | Networking & Online Security | 2 | 06-14-2005 06:23 PM |
| Service Disabling | Jates | Windows Legacy Support (XP and earlier) | 2 | 04-11-2005 09:27 PM |
| Trying to set up an Active Directory, win 2000 | Nayon | Networking & Online Security | 1 | 03-16-2005 08:50 AM |
| A7N8X BIOS not detecting RAID set | foghat46 | Computer Hardware | 5 | 08-22-2003 11:16 PM |
| Server Set up Continued | SteveAH10 | Internet, Web Applications, & The Cloud | 1 | 08-13-2002 10:46 PM |