Setting minimum number of decimal places for std::ostream precision

Posted by Phil Boltt on Stack Overflow See other posts from Stack Overflow or by Phil Boltt
Published on 2010-03-22T02:04:05Z Indexed on 2010/03/22 2:11 UTC
Read the original article Hit count: 386

Filed under:
|
|
|

Hi,

Is there a way to set the "minimum" number of decimal places that a std::ostream will output?

For example, say I have two doubles that I want to print:

double a = 0;
double b = 0.123456789;

I can set my maximum decimal precision so that I output b exactly

std::cout << std::setprecision(9) << b << std::endl;

Is there a way to set "minimum" precision so that

std::cout << a << std::endl;

yields "0.0", not just "0"?

Thanks! Phil

© Stack Overflow or respective owner

Related posts about c++

Related posts about ostream