• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Data Types

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
 
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
 
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;

 
Back
Top