-
Printing Double?
I would like to know how to print double variable if it is assigned a number with alot of digits.
For example, if i were to cout<<(double)100000000<<endl i would get printed "1e+008" printed out. However, I would like to get "100000000" How would I accomplish this?
-
Try
std::cout << std::fixed << 100000000.0 << std::endl;
-
Thanks. is there a way to limit the number of zeros to the needed length? like...
double a = 5;
cout<<fixed<<a<<endl;
would print out something like...
5.0000000000
when simple 5 or 5.0 would be sufficient.
-
-
cout.precision(1);
cout << fixed << a << endl;
e: I'm not quite fast enough
-
C++ unfortunately makes formatting a clumsy affair. In C++ a programmer uses tiny objects called manipulators to change the way the stream behaves. You can find the manipulators that C++ has standard in any reference page: C++ I/O Flags [C++ Reference]. It is important that you become familiar with these.
You can also make your own manipulators.
It is also nice to remove the format after you are done:
cout.resetiosflags( ios_base::showpoint );
Forgetting to do that can make output look strange sometimes when you don't expect it.
-
There is also Boost's Format library which tries to combine the best of both worlds: The Boost Format library
Though I suck at using it (mainly because the documentation is so poor). I only use it for basic formatting.
-