C++ Data Types

cyberphant0m

Member
Oct 21, 2003
99
0
0
Im writing a program for a class, which calculates charges for a mock ISP. I have everything done, but when it outputs results, it gives me something like $23.1. How can I make it so that it doesn't drop the 0 from the right part of the decimal places. Is there a data type I should be using? Currently Im using long doubles. Thanks for the help in advance
 

eklass

Golden Member
Mar 19, 2001
1,218
0
0
data types aren't the problem. what you need is a formatting function, such as printf()
do this:

totalCost = 23.1;
printf("$%.2f", totalCost);

if my code is bad, that's cuase i don't do C very often, but this should at least get you enough to google for more information www.google.com/search?q=printf
 

klah

Diamond Member
Aug 13, 2002
7,070
1
0
In C++ you can include the header <iomanip> and use 'setprecision'.

#include <iostream>
#include <iomanip>
...
cout << fixed; //converts scientific notation to fixed, only needed for large numbers
cout << setprecision(2) << total_cost;