Click to See Complete Forum and Search --> : Minor question about output formatting flags
HighCommander4
March 15th, 2005, 08:36 PM
How do I output a number such that it's never output in scientific notation, but trailing zeros aren't displayed either? I tried
cout << fixed << noshowpoint;
but it still outputs, say, 90 as 90.00000?
bmoodie
March 15th, 2005, 09:45 PM
One way is setprecision, but I don't know the best way:
#include <iostream>
#include <iomanip>
using namespace std;
int main(int cargs, char **vargs) {
cout << setprecision(0) << 90.0 << endl; // outputs "90"
return 0;
}
http://www.cppreference.com/io_flags.html#manipulators
HighCommander4
March 16th, 2005, 07:26 PM
Hmm... setprecision() will cause all numbers to be displayed at the same precision, whereas I need the numbers to be displayed at their default precision, but without trailing zeros.
Basically, I want numbers to be displayed the way they're displayed by default, but without getting into scientific notation when the numbers get big.
Any suggestions?
bmoodie
March 16th, 2005, 07:50 PM
While I'm not sure exactly which settings you want, you can use resetiosflags to turn off anything you don't want:
cout << resetiosflags(ios_base::showpoint | ios_base::scientific | ios_base::fixed) << ...
I hope this helps.
- Bryan
HighCommander4
March 16th, 2005, 08:31 PM
Hmm... that doesn't really work either. 9000000000000000000.0 is still displayed as 9e018...
Basically this is what I need...
90 to be displayed as 90 (no trailing zeros)
9e18 to be displayed as 9000000000000000000 (not in scientific form)
9.85545 to be displayed as 9.85545 (no precision loss)
Is that possible?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.