how to format/truncate

Hi,

Want to format and truncate/round my number at the same time in the program:

Here I want the + sign shown:
1
2
3
4
float n = 3.8;
printf("%+f", n);

=+3.800000


I get success independently with prinftf("%+f","n") and setprecision(x)

I just want the answer to 2 decimal points for which I use
setprecision(2) and the sign "+" is not shown. My goal is to show e.g:
+5.0 as just that
Last edited on
To show the + sign, use showpos.
1
2
    std::cout << std::fixed << std::setprecision(2) << std::showpos;
    std::cout << 3.8;

The settings are 'sticky', once set they remain until/unless deliberately changed .
Worked great, thanx
Topic archived. No new replies allowed.