2

I've had a look at some questions before posting this and couldn't find what I was looking for, so if it's a duplicate I'm sorry.

I have this code:

cout << "The balance at year " << i << " will be " << char(156) << std::setprecision(2)
     << balance << endl;

To my knowledge this should print to the console something like:

£2.00
£100.46

*fyi Above are just examples I know there's nothing to suggest those numbers, but the format should look like that, right?

But here's my output:

£1e+002
£1.1e+002

Why is it doing this?

It was my understanding that using setprecision would show 2 decimal places and nothing more.

Oh and also noticed hardly any questions about printing doubles that said use setprecision actually state that this needs including:

#include <iomanip>
4

3 回答 3

5

setprecision设置精度,位数。

fixed设置固定格式。

我打赌那会解决的。

于 2013-03-27T14:24:30.633 回答
3

用于std::fixed设置固定格式,而不是科学计数法。

cout << "The balance at year " << i << " will be " << char(156) << std::setprecision(2) << std::fixed << balance << endl;

于 2013-03-27T14:25:00.327 回答
2

像这样做:

cout << "The balance at year " << i << " will be " << char(156) << std::setprecision(2) << std::fixed << balance << endl;
于 2013-03-27T14:27:42.447 回答