According to the documentation, setprecision(2) would arrange that. The rest of the padding you'd use setw() for.
Printable View
According to the documentation, setprecision(2) would arrange that. The rest of the padding you'd use setw() for.
I tried that, no dice. Still shows the E notation. This is really annoying. I guess i just have to go find function to round to significant figures. I could use Ceil or Floor, but I want the decimals. heh.Quote:
Originally Posted by Lindley
There's no way it should be using scientific notation if you put the fixed manipulator in there....?
of course it doesn't with Fixed. However, I want a solution that doesn't add zeros. So i'm at the point i'm hacking together a function for it. Now to figure out why converting it to a string yields xxxx.xx but converting it back to a double via istringstream yields the E notation.
Am I right in assuming that you don't want trailing zeros?
123.45
765.2
7868
instead of
123.45
765.20
7868.00
Should specify no trailing zeros. Using setprecision(2) will yield standard money notation. The rest of your formatting should be controlled via setw(); *only* the minimum number of decimal places should be controlled via setprecision().Code:cout << "text" << fixed << right << setw(x) << setprecision(0) << value << endl;
That's what I thought. However, it doesn't work. It just gets put in scientific notation. Here's a screenshot.
http://i96.photobucket.com/albums/l190/c89c/Capture.jpg
It shows E even with 2 setprecision and setw(7).
I suppose it's possible the ordering of the manipulators matters. Try putting "fixed" both to the right and left of setprecision (alternately, not at the same time).....
Don't know what your doing but...
Gives me '1900.00'Code:#include <iostream>
#include <iomanip>
int main()
{
double d = 1900.0;
std::cout << std::fixed << std::setw(8) << std::setprecision(2) << d << std::endl;
return 0;
}
Changing to std::setprecision(0) gives me '1900'
Can you show the line you are actually using rather than having us guess?
Code:cout << "\t\tMonthly Payment:\t" << setprecision(2) << setw(7) << pmt << endl;
....why is std::fixed missing again? You *do* realize that all those extra zeros you were complaining about were the result of having setprecision(>2) before, don't you?
Yes, but this particular line never had setprecision greater than 2 and i've checked and it has exactly two decimal places when it's returned. For some reason, it gets retrned and then output does something weird and puts it in scientific notation.
For example:
Before Return: 1875.11
After Return: 1875.11
When I just do
I get 1875.11. However, once it reaches the line setprecision and setw it does scientific notation for some reason.Code:cout << pmt << endl;
Edit: I just tried putting fixed in and it fixes the problem. I'm still wondering why setprecision and setw would make such a difference.
http://www.cplusplus.com/reference/i...precision.html
If you don't specify fixed-point, then setprecision specifies the maximum number of meaningful digits to display. If the value is 1875.11, and you say setprecision(2), then it's only going to display 1 and 8----and the best way to do that is to write 1.8e3.
Of you do specify fixed-point, then the meaning of setprecision() changes to be the number of digits after the decimal to display.
Gives '1.9e+003'Code:double pmt = 1875.11;
cout << "\t\tMonthly Payment:\t" << setprecision(2) << setw(7) << pmt << endl;
Gives '1875.11'Code:double pmt = 1875.11;
cout << "\t\tMonthly Payment:\t" << fixed << setprecision(2) << setw(7) << pmt << endl;
Somehow, that seems like something our professor should have distinguished. He just used it like it determined decimal places and didn't affect the number at all.Quote:
Originally Posted by Lindley