Reputation: 231
My code is this:
#include <iostream>
int main()
{
using namespace std;
const float dollar = 1.00;
cout << "You have " << dollar*10.00 << " dollars." << endl;
cin.get();
cin.get();
return 0;
}
I am a beginner to C++. I typed this code just playing around and assumed that the console would display "You have 10.00 dollars, but it actually displays that I have "10" and not "10.00" dollars. Why is this?
Upvotes: 4
Views: 19745
Reputation: 101
//cout << setprecision(3) << fixed;
Also one more point: don’t put using namespace std;
inside the main statement,
Add header #include <iomanip>
int main()
{
float dollar = 2.00f;
cout << setprecision(3) << fixed;
std::cout << dollar << " dollars." << endl;
cin.get();
cin.get();
return 0;
}
Upvotes: 1
Reputation: 490128
The standard library already has some code to deal with monetary values. In C++98/03, it's sufficiently painful to use that it's probably not worth the trouble, but in C++11, a couple of I/O manipulators were added that make them quite easy to use.
In the default "C" locale, they probably won't be of much use, but in a nationalized locale, they will format money as you'd normally expect for that locale. For example:
#include <iostream>
#include <iomanip>
int main(){
long double amt;
std::cin.imbue(std::locale(""));
std::cout.imbue(std::locale(""));
std::cin >> std::get_money(amt);
std::cout << std::put_money(amt) << "\n";
}
This uses the ""
locale, which chooses a locale based on how the OS is configured. For example, in my case the OS is set up for US English, so that chooses the US English locale.
Based on the locale, this displays the money as (the programmers guess) money would normally be displayed in that locale. For example, if I enter "10" (with or without some 0's after the decimal point), it prints the value out as 10.00
.
If I prefer, I can specify a locale--for example, I can specify "de" to get a German locale. In this case, the value will (at least with the compiler I have handy) be printed out using German conventions (.
as a thousands separator and ,
as a decimal separator). Also note that I can specify the locale for std::cin
separately from the locale for std::cout
, so I can (for example) set cin
to US English, and cout
to German. If I do so, I can enter 1,234,567.89
as the input, and receive 1.234.567,89
as the output. (Note that although it converts the formatting from US to German convention, it does not automatically convert my input in dollars to Euros at output).
Note that put_money
also automatically restores the previous formatting after it does its thing. For example, if I change the last line above to: std::cout << std::put_money(amt) << "\n";
, then enter 10
as the input, I get 10.00 10
.
Also note that (at least with VC++) std::get_money
actually stores the value as an integer count of cents, which will prevent most rounding errors. So, if I enter 10
what's actually stored is 1000
, which is then re-scaled to display as 10.00
at output.
If you're stuck with an older compiler that doesn't include std::get_money
and std::put_money
, it's probably easiest to just copy their definitions from the standard, put them into a header of your own, and use them anyway. If you don't want to do that, I'd consider defining a money
class that stores an amount of money, and overloads operator>>
and (especially) operator<<
to handle formatting as you see fit, so writing out a monetary value would be something like:
money m(10);
std::cout << m;
...and the operator<<
would handle all the std::setprecision
and such to display that with two places after the decimal point and such.
Upvotes: 2
Reputation: 1937
If you don't format output, cout will cut unnecessary zeroes. You can set precision of output.
cout<<fixed<<setprecision(2)<<dollar<<endl;
Upvotes: 1
Reputation:
Since you are dealing with dollar amounts, you can set the following before writing to cout
:
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
#include <iostream>
int main() {
using namespace std;
const float dollar = 1.00;
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
cout << "You have " << dollar*10.00 << " dollars." << endl;
cin.get();
cin.get();
return 0;
}
Upvotes: 5